MySQL-选择字段值不唯一的所有行
如何选择MySQL中某个字段值不唯一的所有行。例如,我有以下数据:MySQL-选择字段值不唯一的所有行,mysql,Mysql,如何选择MySQL中某个字段值不唯一的所有行。例如,我有以下数据: --------------------------------------- | ID | Name | URL | --------------------------------------- | 1 | Store 1| http://www.store1.com | | 2 | Store 2| http://www.store1.com | | 3 | Store 3|
---------------------------------------
| 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 |
---------------------------------------
如果需要所有原始行,则使用exists:
您可以将内部连接到副本
select t.*
from table t
inner join
(select url from table group by 1 having count(*)>1) duplicates
on duplicates.url=t.url
或者,老派
SELECT DISTINCT x.*
FROM my_table x
JOIN my_table y
ON y.url = x.url
AND y.id <> x.id
ORDER
BY id;
爱死它了!一开始错过了讨论,并认为您可以获得更多的行,但不,这是完美的:您知道该解决方案的性能如何吗?我猜我的子选择解决方案将是最慢的。我喜欢这种老式的学校风格,让我思考D
SELECT DISTINCT x.*
FROM my_table x
JOIN my_table y
ON y.url = x.url
AND y.id <> x.id
ORDER
BY id;