SQL选择具有(部分)重复数据的行
-
10-10-2019 - |
题
我有以下数据库模式:
Product ID | Component | ...
产品ID-外键
组件 - 产品的一部分
由于某些神秘原因,许多记录具有相同的产品ID和组件。是否有SQL查询可以返回具有多个相同组件的所有产品ID和组件?
例如给定下表
| Product ID | Component |
--------------------------
| 1 | c1000 |
| 1 | c1100 |
| 2 | c2000 |
| 2 | c2000 |
| 2 | c2200 |
| 3 | c3000 |
SQL查询应返回:
| Product ID | Component |
--------------------------
| 2 | c2000 |
解决方案
SELECT
ProductId,
Component
FROM
Table
GROUP BY
ProductId,
Component
HAVING
COUNT(*) > 1
其他提示
SELECT ProductId, Component, count(*) Duplicates
from MyTable -- or whatever
group by ProductId, Component
having count(*) > 1
这还将向您展示有多少重复条目。
select "Product ID", Component
from table
group by "Product ID", Component
having count(*) > 1
不隶属于 StackOverflow