user1659464 user1659464 - 1 year ago 64
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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download