

The DISTINCT keyword does not work in the mysql query
source link: https://www.codesd.com/item/the-distinct-keyword-does-not-work-in-the-mysql-query.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The DISTINCT keyword does not work in the mysql query
I have ran into a problem that my mysql query is not working as expected. I am joining 3 tables using union
and from the result i want to ignore the repeating words. For that I have used DISTINCT
keyword. But it is not working as expected.
My query is,
SELECT DISTINCT(catnam), sub2id
FROM tbl_first_page_products f INNER JOIN subcategory_level2 s
ON f.brand_id = s.sub2id
WHERE f.title_cat = 'men'
UNION
SELECT DISTINCT(catnam), sub2id
FROM tbl_third_page_products f INNER JOIN subcategory_level2 s
ON f.brand_id = s.sub2id
WHERE f.title_cat = 'men'
UNION
SELECT DISTINCT(catnam), sub2id
FROM tbl_fourth_page_products f INNER JOIN subcategory_level2 s
ON f.brand_id = s.sub2id
WHERE f.title_cat = 'men'
And my result is,
catnam sub2id
------ ------
Levi's 4
United 1
Reebok 130
Jack 18
Proline 77
Levi's 161
Arrow 284
In the above result Levi's
is repeating. How to ignore this. I need only one Levi's
. Is there any way to avoid this. I am stuck in here.
MySQL DISTINCT with multiple columns
You can use the DISTINCT clause with more than one column. In this case, MySQL uses the combination of all columns to determine the uniqueness of the row in the result set.
http://www.mysqltutorial.org/mysql-distinct.aspx
if you want solve your problem, you can use group_concat() function then you group by catnam for all union queries :
SELECT name ,GROUP_CONCAT(id)
FROM your_table
GROUP BY name ;
so your query can be:
SELECT distinct(catnam) as catnam, GROUP_CONCAT(sub2id )
FROM tbl_first_page_products f INNER JOIN subcategory_level2 s
ON f.brand_id = s.sub2id
WHERE f.title_cat = 'men'
UNION
SELECT distinct(catnam) as catnam, GROUP_CONCAT(sub2id )
FROM tbl_third_page_products f INNER JOIN subcategory_level2 s
ON f.brand_id = s.sub2id
WHERE f.title_cat = 'men'
UNION
SELECT distinct(catnam) as catnam, GROUP_CONCAT(sub2id )
FROM tbl_fourth_page_products f INNER JOIN subcategory_level2 s
ON f.brand_id = s.sub2id
WHERE f.title_cat = 'men'
group by catnam
Recommend
-
58
Learn the best way to use the JPQL DISTINCT keyword with JPA and Hibernate. For entity queries, use the HINT_PASS_DISTINCT_THROUGH query hint.
-
33
作者: 李呈祥,花名司麟),阿里巴巴计算平台事业部EMR团队的高级技术专家,Apache Hive Committer, Apache Flink Committer,深度参与了Hadoop,Hive,Spark,Flink等开源项目的研发工作,对于SQL引擎,分布式系统有较为深入的了解...
-
16
Using HLL++ to speed up count-distinct in massive datasets ...
-
12
Aug 17 2021
-
10
Date comparison does not work (dates are strings) - MySQL advertisements I have a MySQL query that reads: select sum(AmtPd) as...
-
7
SQL SERVER QUERY to get the Number of Distinct column in the table advertisements This is the sample expected result table As you can s...
-
18
Why does not my OData jQuery Dynamic CRM On-Line query work? advertisements I have a web page that needs to obtain informat...
-
5
How Does Semisynchronous MySQL Replication Work? Back to the Blog With the recent release of Percona Xt...
-
3
Posted 2 years agoUp...
-
6
Mysql distinct 和 order by 排序混淆的替代方案...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK