我有两个共享相同ID的表:
表颜色:
id color
---------
1 blue
1 red
1 green
1 blue
2 green
2 yellow
2 orange
3 red
3 orange
3 yellow
3 orange
表名:
id name
---------
1 John
2 Anna
3 Mike
查找任何颜色出现多次的名称的查询是什么。
例如:约翰和迈克是答案,因为约翰有两次蓝色而麦克也有两次橙色。 非常感谢答案 0 :(得分:3)
SELECT A.name, B.color, count(*) 'color count'
FROM Names A JOIN Colors B on A.id = B.id
GROUP BY A.id, B.color
HAVING Count(*)>1
答案 1 :(得分:1)
SELECT name, color, COUNT(*)
FROM Names
JOIN Colors
ON Names.id = Colors.id
GROUP BY name, color
HAVING COUNT(*) > 1
答案 2 :(得分:0)
SELECT DISTINCT n.name FROM Colour c
INNER JOIN Names n ON (n.id = c.id)
GROUP BY n.name, c.colour
HAVING COUNT(*) > 1
答案 3 :(得分:0)
Select n.Name
From names n
Join colors c on c.ID = n.ID
Group by n.Name
Having count(c.color) <> count(distinct c.color)