快速提问...我有一个查询,检查看起来像这样的重复项:
SELECT COUNT( * ) AS repetitions, Name, Phone, ID, categoryid, State
FROM users
GROUP BY Name, Phone, State
HAVING repetitions > 1
ORDER BY ID DESC
这可行,但MySQL返回一组重复项中的第一个ID。例如,假设我有2行。第一行的ID为1,第二行的ID为2,名称,电话和状态具有相同的数据...如何使上述查询返回计数但ID为“2”而不是“1”? / p>
谢谢! ;)
答案 0 :(得分:5)
使用max()聚合函数:
SELECT COUNT(*) AS repetitions, max(ID) FROM users GROUP BY Name, Phone, State HAVING repetitions > 1 ORDER BY ID DESC
答案 1 :(得分:0)
不完美但有效
SELECT COUNT( * ) AS repetitions, Name, Phone, MAX(ID), categoryid, State
FROM users
GROUP BY Name, Phone, State
HAVING repetitions > 1