MySQL SELECT DISTINCT multiple columns

Say I have columns a, bc, d in a table in a MySQL database. What I'm trying to do is select the different values ​​of ALL from the 4 columns in my table (only individual values). I tried things like:

 SELECT DISTINCT a,b,c,d FROM my_table; SELECT DISTINCT a,b,c,d FROM my_table GROUP BY a,b,c,d; 

None of them worked. Can anyone help here?

thank

NOTE I want to highlight columns a, b, cd separately. Fuzzy combination of values

+47
sql mysql distinct
Aug 29 '12 at 23:43 on
source share
6 answers

can this help?

 select (SELECT group_concat(DISTINCT a) FROM my_table) as a, (SELECT group_concat(DISTINCT b) FROM my_table) as b, (SELECT group_concat(DISTINCT c) FROM my_table) as c, (SELECT group_concat(DISTINCT d) FROM my_table) as d 
+37
Aug 29 2018-12-12T00:
source share

I know the question is too old, anyway:

 select a, b from mytable group by a, b 

will give you all the combinations.

+30
Oct 20 '15 at 13:06
source share

Guessing the results you want, maybe this is the query you want, then

 SELECT DISTINCT a FROM my_table UNION SELECT DISTINCT b FROM my_table UNION SELECT DISTINCT c FROM my_table UNION SELECT DISTINCT d FROM my_table 
+16
Aug 29 '12 at 23:55
source share

Another easy way to do this is concat()

 SELECT DISTINCT(CONCAT(a,b)) AS cc FROM my_table GROUP BY (cc); 
+8
Sep 30 '15 at 8:29
source share

Both of your requests are correct and should give the correct answer.

I would suggest the following query to fix your problem.

 SELECT DISTINCT a,b,c,d,count(*) Count FROM my_table GROUP BY a,b,c,d order by count(*) desc 

This is the count count (*) field. This will give you an idea of ​​how many lines were deleted using the group command.

+6
Aug 29 '12 at 23:50
source share

This will give DISTINCT values ​​for all columns:

 SELECT DISTINCT value FROM ( SELECT DISTINCT a AS value FROM my_table UNION SELECT DISTINCT b AS value FROM my_table UNION SELECT DISTINCT c AS value FROM my_table ) AS derived 
+5
Mar 04 '15 at 22:33
source share



All Articles