0
SELECT T2.ITEM_ID AS item_ITEM_ID 
FROM types T 
  JOIN items T2
    ON T.ITEM_ID = T2.ITEM_PARENT_ID
WHERE T.ITEM_TYPE = 'I' 
 AND T2.ITEM_TYPE = 'I' 

此查询从 T2 返回具有重复 T2.ITEM_PARENT_ID 的行。
我将如何指定此查询以仅返回具有唯一 T2.ITEM_PARENT_IDs 的行?

4

1 回答 1

1

计算重复次数并在大于 1 时过滤掉。

SELECT T2.ITEM_ID AS item_ITEM_ID, COUNT(*) dups
FROM types T 
  JOIN items T2
    ON T.ITEM_ID = T2.ITEM_PARENT_ID
WHERE T.ITEM_TYPE = 'I' 
 AND T2.ITEM_TYPE = 'I' 
GROUP BY item_ITEM_ID
HAVING dups = 1

演示

于 2013-08-16T21:35:10.033 回答