[英]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. 我想做的是,如果同一ID下的desc不同,则显示混合的desc,否则显示desc的值。 Just like below 就像下面
id desc
1 a/b/c
2 a
How could I do it in one sql ? 我怎么能在一个SQL中做到这一点?
enter code here 在这里输入代码
I think you want group_concat()
with the distinct
modifier: 我认为您希望group_concat()
具有distinct
修饰符:
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
). 请注意, desc
对于列而言是一个不好的名字,因为它是一个SQL关键字( order by
)。 It is best to avoid grammatical elements when choosing names. 选择名称时,最好避免使用语法元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.