Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何通过重复键查找唯一行_Mysql_Sql - Fatal编程技术网

Mysql 如何通过重复键查找唯一行

Mysql 如何通过重复键查找唯一行,mysql,sql,Mysql,Sql,我需要为一个特定的模型找到按汽车和颜色唯一的行,它有超过1种颜色 Car Model Color --------------------- Skoda Fabia Red Skoda Fabia Blue Skoda Fabia Red Skoda Octavia Red 但这不起作用。我想你想要: select distinct c.* from cars c where model = 'Fabia' and exists (select 1

我需要为一个特定的模型找到按汽车和颜色唯一的行,它有超过1种颜色

Car Model Color --------------------- Skoda Fabia Red Skoda Fabia Blue Skoda Fabia Red Skoda Octavia Red 但这不起作用。

我想你想要:

select distinct c.*
from cars c
where model = 'Fabia' and
      exists (select 1
              from cars c2
              where c2.model = c.model and
                    c2.car = c.car and
                    c2.color <> c.color
             );
试试这个:

SELECT distinct a.* FROM data as a 
JOIN 
(SELECT Car, Model,count(1)
 FROM data 
 GROUP BY Car, Model 
 HAVING count(1) > 1) as b
ON a.Model=b.Model AND a.Car=b.Car;
我有一个解决办法:

SELECT *,group_concat(DISTINCT Color) FROM data WHERE model='Fabia' GROUP BY Car HAVING count(DISTINCT Color)>1
从数据中选择*,组_concat(不同颜色),其中模型='Fabia'按车分组,计数(不同颜色)>1
从数据中选择汽车,模型,颜色,其中模型='Fabia'按车分组,模型,颜色计数(*)>1
定义不起作用?我假设您没有得到正确的结果,或者错误地只得到了一个完整的组?我可以得到很好的结果,但它们也显示了少于两个唯一记录的行。请记住,
group\u concat(…)
可能需要
设置会话组concat\u max\u len=红色Fabia
,因为内部SQL将在模型中查找重复的颜色->哦,我找到了,我实际上交换了字段名,让我更正它@雷蒙德尼兰德编辑了它
select distinct c.*
from cars c
where model = 'Fabia' and
      exists (select 1
              from cars c2
              where c2.model = c.model and
                    c2.car = c.car and
                    c2.color <> c.color
             );
select car, model, group_concat(distinct color)
from cars c2
where model = 'Fabia'
group by car, model
having count(distinct color) > 1;
SELECT distinct a.* FROM data as a 
JOIN 
(SELECT Car, Model,count(1)
 FROM data 
 GROUP BY Car, Model 
 HAVING count(1) > 1) as b
ON a.Model=b.Model AND a.Car=b.Car;
SELECT *,group_concat(DISTINCT Color) FROM data WHERE model='Fabia' GROUP BY Car HAVING count(DISTINCT Color)>1