简体   繁体   中英

Finding Duplicates with SQL?

I want to know if my database contains duplicates. If not, I know that my code is working.

My database looks like this:

----------------------------
| id | name | price | link  | 
|---------------------------|
| 202| test | 34.00 | googl |
| 203| halo | 22.00 | bing  |
| 204| hovo | 31.00 | link  |
| 205| test | 34.00 | googl |
-----------------------------

You can see that the values of name, price and link are they same but the id is different. I thought about something like group the name, price and link and count it with having. Like this for example:

SELECT email, 
COUNT(email) AS NumOccurrences
FROM users
GROUP BY email
HAVING ( COUNT(email) > 1 )

But my problem is how can I group by 3 attributes like in my table. By name, price and link?

You can specify 3 columns in GROUP BY clause, eg:

SELECT name, price, link 
FROM users
GROUP BY name, price, link
HAVING COUNT(email) > 1;

You can group by more than one field at a time.

SELECT a, b, c, COUNT(d) FROM someTable GROUP BY a, b, c HAVING COUNT(d) > 1

Hope this helps.

Group by multiple columns, and the count the groups size:

SELECT name, price, link, Count(*) as cnt 
FROM table_name
GROUP BY name, price, link
where cnt>1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM