104

Let's say I have columns a, b c, d in a table in a MySQL database. What I'm trying to do is to select the distinct values of ALL of these 4 columns in my table (only the distinct values). I tried stuff 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 those worked. Can anybody help out here?

Thank you

NOTE I want the distinct values of the columns a, b, c d separately. Not the distinct combination of values

user765368
  • 19,590
  • 27
  • 96
  • 167

10 Answers10

116

I know that the question is too old, anyway:

SELECT a, b
FROM mytable
GROUP BY a, b;

This will give your all the combinations.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Nicolas Castro
  • 1,436
  • 1
  • 14
  • 15
60

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
Nesim Razon
  • 9,684
  • 3
  • 36
  • 48
  • 9
    What if I need only distinct results from first two column but need to show all columns... – Uday Hiwarale Dec 17 '13 at 11:32
  • 2
    ncastro's is better. Cooler, easier, anyway. – Buffalo Aug 30 '16 at 08:24
  • Hey Nesim, Thanks! by the way if "my_table" is a whole query how can I call it by name and recall it again and again – Tomer Apr 03 '17 at 18:52
  • But this result gives comma separated value. How can I get rid of this problems. – Chayan Biswas Aug 04 '17 at 21:09
  • Chayan, that's a different issues, but it's `REPLACE(group_concat(DISTINCT d),',',' ')` – Slam Oct 06 '17 at 18:10
  • Mysql Error as : #1247 - Reference 'a' not supported (forward reference in item list). – bansal Jun 18 '18 at 11:36
  • Note that this will silently truncate each resulting column to `group_concat_max_len` characters long, [which by default](https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_group_concat_max_len) is only 1024 characters - you could lose data! – thenickdude Jun 20 '18 at 02:34
  • distinct results from first tow rows `SELECT (SELECT GROUP_CONCAT(a) FROM (SELECT DISTINCT a FROM my_table LIMIT 2) AS a) AS a, (SELECT GROUP_CONCAT(a) FROM (SELECT DISTINCT b FROM my_table LIMIT 2) AS b) AS b;` – free斩 Jun 24 '20 at 16:00
25

Another simple way to do it is with concat()

SELECT DISTINCT(CONCAT(a,b)) AS cc FROM my_table GROUP BY (cc);
Tamir Vered
  • 10,187
  • 5
  • 45
  • 57
Kelhen
  • 251
  • 3
  • 7
22

Taking a guess at the results you want so 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
Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
  • 1
    This works too, but I like the separation between the results better in Nesim Razon's answer. Thank you – user765368 Aug 30 '12 at 00:00
  • Cool - I was going to look at combining them – Adrian Cornish Aug 30 '12 at 00:05
  • 6
    If you want DISTINCT values across all the columns, you have to wrap this query in another SELECT statement like this: `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` – T. Brian Jones Mar 04 '15 at 22:31
  • `SELECT e,f FROM ( SELECT DISTINCT zdjh AS e FROM `tj_xhqd` UNION SELECT DISTINCT sjsj AS f FROM `tj_xhqd` UNION SELECT DISTINCT xhqd AS g FROM `tj_xhqd`) a` gives me `Error Code: 1054 Unknown column 'f' in 'field list'` – Moeez Jul 04 '18 at 07:14
11

This will give DISTINCT values across all the 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
T. Brian Jones
  • 13,002
  • 25
  • 78
  • 117
9

Both your queries are correct and should give you the right answer.

I would suggest the following query to troubleshoot your problem.

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

That is add count(*) field. This will give you idea how many rows were eliminated using the group command.

TheTechGuy
  • 16,560
  • 16
  • 115
  • 136
  • I want disctinct values of a's, b's, c's and d's, not distinct combination of values – user765368 Aug 29 '12 at 23:51
  • @hmd: Thanks for this solution. Exactly what I needed. Is there is way to include a `where` condition? I wanted only rows where the count was greater than 1. Tried `select distinct a,b,c,count(*) from MyTempTable group by a,b,c order by count(*) desc where count(*)>1;` but it didn't work. – Nav Jun 01 '16 at 09:53
  • @Nav42 you need to use `having count(*) > 1` not `where` as this is aggregate query. Btw my answer does not exactly addresses the OP question as he is outlined above. Anyways glad to help. – TheTechGuy Jun 01 '16 at 10:36
  • Works. Thank you very much. select `distinct a,b,c,count(*) from MyTable group by a,b,c having count(*) > 1 order by count(*) desc ` – Nav Jun 02 '16 at 05:12
4

select distinct concat( colA , ' ' , colB , ' ' , colC ) from tableName;

CWD Subs
  • 191
  • 1
  • 2
  • 7
  • 1
    This answer was reviewed in the [Low Quality Queue](https://stackoverflow.com/help/review-low-quality). Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / **better** than existing answers. [From Review](https://stackoverflow.com/review/low-quality-posts/32586107) – Trenton McKinney Aug 28 '22 at 15:29
  • It doesn't warrant more explanation, it's self-explanatory. Delete the answer if you don't like it, I added it because none of the other answers was that simple – CWD Subs Sep 03 '22 at 20:04
0

You can simply use this to get back one row with four comma separated lists with all values.

SELECT
  GROUP_CONCAT(DISTINCT `a`) AS `as`,
  GROUP_CONCAT(DISTINCT `b`) AS `bs`,                
  GROUP_CONCAT(DISTINCT `c`) AS `cs`,                
  GROUP_CONCAT(DISTINCT `d`) AS `ds`
FROM
  `my_table`;

If you need an other separator than the comma, add the SEPARATOR option to GROUP_CONCAT.

Ludwig
  • 3,580
  • 2
  • 20
  • 24
0

Based on your answers I made the same but for extracting a json column vars with arrays of 8 values all merged in 1 column with quotes trimed distinct values.

SELECT DISTINCT TRIM(BOTH '"' FROM T1.`vars`) FROM (
    SELECT JSON_EXTRACT(`vars`, '$[1]') AS `vars` 
         FROM my_table
         WHERE `vars` != '[]'
         UNION 
    SELECT JSON_EXTRACT(`vars`, '$[2]') AS `vars`
         FROM my_table
         WHERE `vars` != '[]'
         UNION 
    SELECT JSON_EXTRACT(`vars`, '$[3]') AS `vars`
         FROM my_table
         WHERE `vars` != '[]'
         UNION 
    SELECT JSON_EXTRACT(`vars`, '$[4]') AS `vars`
         FROM my_table
         WHERE `vars` != '[]'
         UNION 
    SELECT JSON_EXTRACT(`vars`, '$[5]') AS `vars`
         FROM my_table
         WHERE `vars` != '[]'
         UNION 
    SELECT JSON_EXTRACT(`vars`, '$[6]') AS `vars`
         FROM my_table
         WHERE `vars` != '[]'
         UNION 
    SELECT JSON_EXTRACT(`vars`, '$[7]') AS `vars`
         FROM my_table
         WHERE `vars` != '[]'
         UNION 
    SELECT JSON_EXTRACT(`vars`, '$[8]') AS `vars`
         FROM my_table
         WHERE `vars` != '[]'
) T1
WHERE `vars` IS NOT NULL
AND `vars` != 'false';

Hope it can helps...

Meloman
  • 3,558
  • 3
  • 41
  • 51
0

i do NOT know the values of your columns are ids as mine, so here is my solution if it fits you

SELECT DISTINCT (sender+receiver) AS smart_sum, sender, receiver 
FROM chatTable 
WHERE sender=1 OR receiver=1 
GROUP BY smart_sum

I have a chat table, sender and receiver columns represent the user_id of each, so the sum of them is actually unique.

Transistor
  • 193
  • 12
Omar N Shamali
  • 503
  • 5
  • 11