patternsqlCritical
select rows where column contains same data in more than one record
Viewed 0 times
rowssamecolumnwheremorethanonerecordcontainsselect
Problem
I have a table that has a column called
Here's what I've got:
article_title. Let's say the table name is articles. I need to find out the records where the article_title data is the same on more than one record.Here's what I've got:
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)Solution
HAVING is a great aggregate filter. (http://dev.mysql.com/doc/refman/5.6/en/group-by-extensions.html) For example, select the article_titles with more than on occurrence:
Adding columns to the SELECT and GROUP BY clauses allow you to locate duplicates based on a composite key of multiple columns.
SELECT count(*), article_title
FROM articles
GROUP BY article_title
HAVING COUNT(*) > 1;Adding columns to the SELECT and GROUP BY clauses allow you to locate duplicates based on a composite key of multiple columns.
Code Snippets
SELECT count(*), article_title
FROM articles
GROUP BY article_title
HAVING COUNT(*) > 1;Context
StackExchange Database Administrators Q#54187, answer score: 53
Revisions (0)
No revisions yet.