Select duplicates query returns duplicates MYSQL

流过昼夜 提交于 2019-12-11 01:57:07

问题


I have the following query that I want to return all the duplicated rows to the user for them to decide which one to use:

SELECT * FROM `table` 
INNER JOIN (SELECT * FROM table GROUP BY barcode HAVING (COUNT(id) > 1)) dup 
ON `table`.`column` = `dup`.`column`

Each part of this query appears to return the correct response but when adding the 2 together I am getting duplicates of the duplicates if that makes sense. Any ideas on what is going on?

My table data

+----+------------------------+---------------------+---------------------+
| id |        barcode         |     created_at      |     updated_at      |
+----+------------------------+---------------------+---------------------+
|  2 | 0000000002800000008741 | 2015-10-05 12:04:52 | 2015-10-05 12:04:52 |
|  3 | 0000000002800000008738 | 2015-10-05 13:15:02 | 2015-10-05 13:37:51 |
|  4 | 0000000002800000008741 | 2015-10-05 13:22:38 | 2015-10-05 13:22:38 |
|  5 | 0000000002800000008738 | 2015-10-05 13:22:38 | 2015-10-05 13:22:38 |
|  6 | 0000000002800000008738 | 2015-10-05 13:23:29 | 2015-10-05 13:23:29 |
|  7 | 0000000002800000008738 | 2015-10-05 13:24:49 | 2015-10-05 13:24:49 |
|  8 | 0000000002800000008741 | 2015-10-05 13:24:49 | 2015-10-05 13:24:49 |
|  9 | 0000000002800000008738 | 2015-10-05 13:37:51 | 2015-10-05 13:37:51 |
| 10 | 0000000002800000008741 | 2015-10-05 13:37:51 | 2015-10-05 13:37:51 |
+----+------------------------+---------------------+---------------------+

The output of the above query

+----+------------------------+---------------------+---------------------+
| id |        barcode         |     created_at      |     updated_at      |
+----+------------------------+---------------------+---------------------+
|  2 | 0000000002800000008741 | 2015-10-05 12:04:52 | 2015-10-05 12:04:52 |
|  3 | 0000000002800000008738 | 2015-10-05 13:15:02 | 2015-10-05 13:37:51 |
|  4 | 0000000002800000008741 | 2015-10-05 13:22:38 | 2015-10-05 13:22:38 |
|  5 | 0000000002800000008738 | 2015-10-05 13:22:38 | 2015-10-05 13:22:38 |
|  6 | 0000000002800000008738 | 2015-10-05 13:23:29 | 2015-10-05 13:23:29 |
|  7 | 0000000002800000008738 | 2015-10-05 13:24:49 | 2015-10-05 13:24:49 |
|  8 | 0000000002800000008741 | 2015-10-05 13:24:49 | 2015-10-05 13:24:49 |
|  9 | 0000000002800000008738 | 2015-10-05 13:37:51 | 2015-10-05 13:37:51 |
| 10 | 0000000002800000008741 | 2015-10-05 13:37:51 | 2015-10-05 13:37:51 |
| 11 | 0000000002800000008738 | 2015-10-05 13:39:05 | 2015-10-05 13:39:05 |
| 12 | 0000000002800000008741 | 2015-10-05 16:16:41 | 2015-10-05 16:16:41 |
| 13 | 0000000002800000008738 | 2015-10-05 16:39:56 | 2015-10-05 16:39:56 |
| 14 | 0000000002800000008738 | 2015-10-06 10:12:32 | 2015-10-06 10:12:52 |
| 15 | 0000000002800000008741 | 2015-10-06 10:17:30 | 2015-10-06 10:17:30 |
| 16 | 0000000002800000008741 | 2015-10-05 16:50:40 | 2015-10-05 16:50:40 |
+----+------------------------+---------------------+---------------------+

来源:https://stackoverflow.com/questions/33104074/select-duplicates-query-returns-duplicates-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!