0

I am trying to develope a query to fetch the rows having duplicate values, i need to fetch both records i.e duplicating record and the real one, for example

table

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

The query result should be:

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

Please anyone help me!

1

3 Answers 3

2

Query:

select * from 
table where keyword in
(select keyword 
from table
group by keyword 
having count(keyword)>1)
Sign up to request clarification or add additional context in comments.

2 Comments

@Saritha You should try consider it as an answer if it worked.
@Luv Guess she isn't accepting any answers. See her questions history! :O
0

One way to do it:

SELECT * 
FROM `table` t1
WHERE 
    (SELECT COUNT(*) FROM `table` t2 WHERE t2.keyword = t1.keyword) > 1

And another way:

SELECT t1.*
FROM `table` t1
JOIN `table` t2 ON t1.keyword = t2.keyword
WHERE t1.id != t2.id

Comments

0

This might help:

SELECT t1.id, t1.keyword                                     
FROM table t1              
INNER JOIN table t2                   
ON t1.id != t2.id           
AND t1.keyword=t2.keyword

Tested on SQL Fiddle http://sqlfiddle.com/#!2/44dbb/1/0

2 Comments

Thanks for the reply! But i am fetching records from only one table, not two tables, so i think this INNER JOIN is not needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.