如何在MySQL中选择特定字段值不唯一的所有行。例如,我有以下数据:
---------------------------------------
| 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 |
---------------------------------------
在此我想要返回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 |
---------------------------------------
答案 0 :(得分:7)
或,老派......
SELECT DISTINCT x.*
FROM my_table x
JOIN my_table y
ON y.url = x.url
AND y.id <> x.id
ORDER
BY id;
答案 1 :(得分:3)
如果您想要所有原始行,请使用exists
:
select t.*
from table t
where exists (select 1 from table t2 where t2.url = t.url and t2.id <> t.id);
答案 2 :(得分:1)
您可以内部联接到您的副本。
select t.*
from table t
inner join
(select url from table group by 1 having count(*)>1) duplicates
on duplicates.url=t.url