简体   繁体   English

MySQL - 选择字段值不唯一的所有行

[英]MySQL - Select all rows where a field value is not unique

How can I select all rows in MySQL where a particular field value is not unique. 如何在MySQL中选择特定字段值不唯一的所有行。 For example I have the following data: 例如,我有以下数据:

---------------------------------------
| ID | Name   |         URL           |
---------------------------------------
| 1  | Store 1| http://www.store1.com |
| 2  | Store 2| http://www.store1.com |
| 3  | Store 3| http://www.store3.com |
| 4  | Store 4| http://www.store4.com |
| 5  | Store 5| http://www.store4.com |
---------------------------------------

In this I would want to return the following where the URL field has duplicates: 在这里我想要返回URL字段有重复的以下内容:

---------------------------------------
| ID | Name   |         URL           |
---------------------------------------
| 1  | Store 1| http://www.store1.com |
| 2  | Store 2| http://www.store1.com |
| 4  | Store 4| http://www.store4.com |
| 5  | Store 5| http://www.store4.com |
---------------------------------------

or, old school... 或者,老派......

SELECT DISTINCT x.* 
           FROM my_table x 
           JOIN my_table y 
             ON y.url = x.url 
            AND y.id <> x.id 
          ORDER 
             BY id;

If you want all the original rows, then use exists : 如果您想要所有原始行,则使用exists

select t.*
from table t
where exists (select 1 from table t2 where t2.url = t.url and t2.id <> t.id);

You can inner join to your duplicates. 您可以内部联接到您的副本。

select t.* 
from table t
inner join
(select url from table group by 1 having count(*)>1) duplicates
on duplicates.url=t.url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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