0

我正在尝试开发一个查询来获取具有重复值的行,我需要获取两个记录,即重复记录和真实记录,例如

桌子

id  keyword
-------------------
1   Apple
2   Orange
3   Apple
4   Grape
5   Banana
6   Grape

查询结果应该是:

id  keyword
-------------------
1   Apple
3   Apple
4   Grape
6   Grape

请任何人帮助我!

4

3 回答 3

2

询问:

select * from 
table where keyword in
(select keyword 
from table
group by keyword 
having count(keyword)>1)
于 2013-05-20T07:20:51.623 回答
0

这可能会有所帮助:

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

于 2013-05-20T07:18:25.473 回答
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
于 2013-05-20T07:24:00.500 回答