桌子:
locations
---------
contract_id INT
order_position INT # I want to find any bad data in this column
我想order_position
在给定的contract_id
.
因此,例如任何情况,例如如果两locations
行的 acontract_id
为“24845”,并且两者的 aorder_position
为“3”。
我正在使用 MySQL 5.1。
select contract_id, order_position
from locations
group by contract_id, order_position
having count(*) > 1
SELECT contract_id, order_position, count(*)
FROM locations
GROUP BY contract_id, order_position
HAVING count(*) > 1
HAVING 子句可以显示存在重复项的实例:
SELECT *
FROM locations
GROUP BY contract_id, order_position
HAVING COUNT(*) > 1
SELECT contract_id, order_position, COUNT(order_position) AS cnt
FROM locations
GROUP BY contract_id, order_position
HAVING cnt > 1;
为什么COUNT(column)
通常比COUNT(*)
(好吧,这只是为了区分我的答案:D)