我正在尝试开发一个查询来获取具有重复值的行,我需要获取两个记录,即重复记录和真实记录,例如
桌子
id keyword
-------------------
1 Apple
2 Orange
3 Apple
4 Grape
5 Banana
6 Grape
查询结果应该是:
id keyword
-------------------
1 Apple
3 Apple
4 Grape
6 Grape
请任何人帮助我!
询问:
select * from
table where keyword in
(select keyword
from table
group by keyword
having count(keyword)>1)
这可能会有所帮助:
SELECT t1.id, t1.keyword
FROM table t1
INNER JOIN table t2
ON t1.id != t2.id
AND t1.keyword=t2.keyword
在 SQL Fiddle 上测试 http://sqlfiddle.com/#!2/44dbb/1/0
一种方法:
SELECT *
FROM `table` t1
WHERE
(SELECT COUNT(*) FROM `table` t2 WHERE t2.keyword = t1.keyword) > 1
另一种方式:
SELECT t1.*
FROM `table` t1
JOIN `table` t2 ON t1.keyword = t2.keyword
WHERE t1.id != t2.id