MySQL选择检测组内互斥的行冲突(MySQL select to detect mutually exclusive row conflicts within groups)

编程入门 行业动态 更新时间:2024-10-23 13:28:25
MySQL选择检测组内互斥的行冲突(MySQL select to detect mutually exclusive row conflicts within groups)

使用以下示例数据,我想创建一个查询,该查询可以找到item / desc重复的位置,并且它们各自的数据字段不是互斥的。 含义...对于给定的item / desc组合,列中只能有1个数据值。

样本表记录:

id | item | desc | data1 | data2 | data3 ----+-------+-------+-------+-------+------- 1 | 1 | cat | a | | 2 | 1 | cat | | b | 3 | 1 | cat | | e | 4 | 2 | dog | a | | 5 | 2 | dog | | h | f 6 | 3 | apple | k | | m 7 | 3 | worm | a | g | x 8 | 4 | rock | p | | s 9 | 4 | rock | | | s 10 | 4 | rock | | t | z

预期的查询结果:

item | desc -------+------- 1 | cat (because of conflict in data2 with b & e) 4 | rock (because of conflict in data3 with s,s & z

With the following sample data, I want to create a query that can find where item/desc is duplicate and their respective data fields are not mutually exclusive. Meaning... there can be only 1 data value in column for a given combination of item/desc.

Sample table records:

id | item | desc | data1 | data2 | data3 ----+-------+-------+-------+-------+------- 1 | 1 | cat | a | | 2 | 1 | cat | | b | 3 | 1 | cat | | e | 4 | 2 | dog | a | | 5 | 2 | dog | | h | f 6 | 3 | apple | k | | m 7 | 3 | worm | a | g | x 8 | 4 | rock | p | | s 9 | 4 | rock | | | s 10 | 4 | rock | | t | z

Expected query result:

item | desc -------+------- 1 | cat (because of conflict in data2 with b & e) 4 | rock (because of conflict in data3 with s,s & z

最满意答案

这应该工作:

select distinct item, `desc` from table group by item, `desc` HAVING count(distinct data1) > 1 or count(distinct data2) > 1 or count(distinct data3) > 1

This should work:

select distinct item, `desc` from table group by item, `desc` HAVING count(distinct data1) > 1 or count(distinct data2) > 1 or count(distinct data3) > 1

更多推荐

本文发布于:2023-08-04 12:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416090.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:冲突   互斥   detect   select   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!