我有以下数据库架构:
Product ID | Component | ...
产品ID - 外键
组件 - 产品的一部分
出于某些奥术原因,许多唱片都拥有相同的产品ID&零件。是否有SQL查询将返回所有产品ID&组件有多个相同的组件?
E.g。给出下表
| Product ID | Component |
--------------------------
| 1 | c1000 |
| 1 | c1100 |
| 2 | c2000 |
| 2 | c2000 |
| 2 | c2200 |
| 3 | c3000 |
SQL查询应返回:
| Product ID | Component |
--------------------------
| 2 | c2000 |
答案 0 :(得分:2)
SELECT ProductId, Component, count(*) Duplicates
from MyTable -- or whatever
group by ProductId, Component
having count(*) > 1
这也会显示有多少重复条目。
答案 1 :(得分:2)
SELECT
ProductId,
Component
FROM
Table
GROUP BY
ProductId,
Component
HAVING
COUNT(*) > 1
答案 2 :(得分:1)
select "Product ID", Component
from table
group by "Product ID", Component
having count(*) > 1