2013-06-12 59 views
0

表:如何編寫查詢以在分組行中查找重複值?

locations 
--------- 
contract_id INT 
order_position INT # I want to find any bad data in this column 

我想給定contract_id內找到任何重複order_position秒。

因此,例如,像如果兩個locations行過的「24845」一個contract_id兩者具有「3」的order_position任何情況。

我正在使用MySQL 5.1。

回答

1

HAVING子句可以告訴你在哪裏重複存在的實例:

SELECT * 
FROM locations 
GROUP BY contract_id, order_position 
HAVING COUNT(*) > 1 
3
select contract_id, order_position 
from locations 
group by contract_id, order_position 
having count(*) > 1 
1
SELECT contract_id, order_position, count(*) 
FROM locations 
GROUP BY contract_id, order_position 
HAVING count(*) > 1 
相關問題