我的以下查询不返回重复记录(Model_Number),是否有原因。应该返回所有重复的model_number,使用以下查询返回所有唯一.Mysql总共有2,17,092条记录
SELECT AZ_Code, Model_Number
FROM dumpdata
GROUP BY Model_Number
HAVING COUNT( * ) >1
答案 0 :(得分:1)
以前的答案应该有效,但我希望这个答案要快得多(因为MySQL通常比在其他地方使用它们更有效地加入子查询,例如在WHERE子句中)。
SELECT dd.AZ_Code, dd.Model_Number
FROM dumpdata AS dd
INNER JOIN (
SELECT Model_Number
FROM dumpdata
GROUP BY Model_Number
HAVING COUNT(*) > 1
) AS repeatedModels
ON dd.Model_Number = repeatedModels.Model_Number
答案 1 :(得分:0)
group by
子句意味着您只需要按每个唯一值分配一个结果(并且您可以查询不在其中的字段是MySQL奇怪的事实)。您可以使用in
运算符来处理:
SELECT AZ_Code, Model_Number
FROM dumpdata
WHERE Model_Number IN (SELECT Model_Number
FROM dumpdata
GROUP BY Model_Number
HAVING COUNT(*) > 1)
答案 2 :(得分:0)
查看重复项的一种方法
SELECT d.AZ_Code, d.Model_Number
FROM dumpdata d
WHERE
(SELECT COUNT(*) FROM dumpdata WHERE d.Model_Number=dumpdata.Model_Number)>1