user1659464 user1659464 - 1 month ago 5
SQL Question

How to check if the column value are the same among multiple record with one sql

suppose there is a table

id desc
1 a
1 b
1 c
1 a
2 a
2 a

what I want to do is to show a mix of desc if the desc under the same id are different, otherswise show the value of the desc. Just like below

id desc
1 a/b/c
2 a

How could I do it in one sql ?

enter code here


I think you want group_concat() with the distinct modifier:

select id, group_concat(distinct `desc` separator '/') as `desc`
from t
group by id;

Note that desc is a bad name for a column because it is a SQL keyword (think order by). It is best to avoid grammatical elements when choosing names.