Answers:
HAVING是一个很棒的聚合筛选器。(http://dev.mysql.com/doc/refman/5.6/en/group-by-extensions.html)例如,选择出现次数大于的article_titles:
SELECT count(*), article_title
FROM articles
GROUP BY article_title
HAVING COUNT(*) > 1;
在SELECT和GROUP BY子句中添加列可让您基于多个列的复合键来查找重复项。
您的问题可以通过以下查询解决:
SELECT *
FROM article
WHERE article_title IN (SELECT *
FROM (SELECT article_title
FROM article
GROUP BY article_title
HAVING COUNT(article_title) > 1)
AS a);
有一个表,该表的列名为article_title。假设表名称是article。我需要找出多个记录上的article_title数据相同的记录。
在我看来,您还需要具有ID,因为您希望基于article_title
重复的记录来查找记录
带有GROUP BY的基本MIN / MAX(如果重复超过2个,您将错过id)
SELECT
MIN(id) -- for FIFO id's (first id by duplicate)
, MAX(id) -- for LIFO id's (last id by duplicate)
, article_title
, COUNT(*)
FROM
articles
WHERE -- Maybe to filter out '' or IS NOT NULL
article_title != '' AND article_title IS NOT NULL
GROUP BY
article_title ASC
HAVING
COUNT(*) >= 2
;
或返回非规范化以为LIFO ID(旧ID重复生成)生成CSV,但您知道所有ID在此处。
SELECT
GROUP_CONCAT(id ORDER BY ASC SEPARATOR ',') -- change to DESC if want the last record first
, article_title
, COUNT(*)
FROM
articles
GROUP BY
article_title ASC
HAVING
COUNT(*) >= 2
;
GROUP BY
:mysqlperformanceblog.com/2006/09/06/...