选择行,其中列包含多个记录中的相同数据


28

我有一个表,其中有一个名为的列article_title。假设表格名称为articles。我需要找出多个记录中article_title数据相同的记录。

这是我得到的:

select a.* 
from articles a 
where a.article_title = (select article_title 
                         from articles 
                         where article_title = a.article_title 
                         AND a.id <> articles.id)

Answers:


35

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子句中添加列可让您基于多个列的复合键来查找重复项。


2
@jkushner:但怪癖当心(或错误因为有些人可能看到它)在MySQL的执行GROUP BYmysqlperformanceblog.com/2006/09/06/...
a_horse_with_no_name

4

您的问题可以通过以下查询解决:

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);

这是正确的答案,因为它实际上返回包含重复项的行
etayluz

3

有一个表,该表的列名为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
;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.