簡體   English   中英

SQL選擇具有(部分)重復數據的行

[英]SQL select rows that have (partially) duplicated data

我有以下數據庫架構:

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, count(*) Duplicates
 from MyTable  --  or whatever
 group by ProductId, Component
 having count(*) > 1

這還將顯示有多少重復的條目。

SELECT
  ProductId,
  Component
FROM
  Table
GROUP BY
  ProductId,
  Component
HAVING
  COUNT(*) > 1
select "Product ID", Component
from table
group by "Product ID", Component
having count(*) > 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM