简体   繁体   English

如何用一个SQL检查多个记录之间的列值是否相同

[英]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.

相关问题 查找同一列 SQL 中记录值之间的差异 - finding difference among record values in same column SQL 如何查询具有第v.2列之一相同值的行中具有最高列值的行 - How to query for rows that have highest column value among rows that have same value for one of the columns v.2 如何检查一个记录中的一组值中是否存在一个值 - How to check whether a value exists among a set of values in a record in mysql 如何在SQL中检查子项的所有父记录的列值 - How to check the column value of all parent record of a child in SQL SQL中同一行的列值中第二高的值 - Second highest value among column values of same row in SQL 需要在MYSQL中的同一日期的多个记录中搜索列中缺少一个值的记录 - Need to search record with missing one value in column out of multiple record for the same date in MYSQL 如何在T-SQL的一列中选择具有相同值的多个条目 - How to select multiple entries with the same value in one column in T-SQL 如何使用SQL检查一列中的字符串值是否部分包含在另一列的字符串值中? - How to check if string value in one column is partially contained in string value of another column using SQL? php sql check value为每条正面记录打印一行 - php sql check value print one line for each positive record select 如何在 mysql 的一条记录中出现多个值? - How select multiple value in one record in mysql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM