Database:
id name
1 Supriya
2 Himanshu
3 Deepika
4 Himanshu
5 sonu
6 Sonu

I am using name field to fetch the datas and my OUTPUT should be as

id name
2 Himanshu
4 Himanshu
1 Supriya
3 Deepika

what query should i use

Recommended Answers

All 6 Replies

It isn't clear exactly what you're after here (based your sample output anyway). If you want duplicate names to be listed together you just need to use 'ORDER BY name' at the end of your query and it will be listed in alphabetical order by the name column.

SELECT * FROM database WHERE id <= 4;

Ayrton, what does that have to do with finding duplicates in the table as the post title mentions?

Hello,

It depends on what you are trying to do. If you are trying to find out if there are duplicates and how many then this will work:

select name, 
count(name) as NumberOfEntries
from database
group by name
having NumberOfEntries > 1

IF you are trying to find out the id's of the records with more than one entry then that can be done several ways depending on what you really want. Let us know.

since i am using this in php to fetch from database , when i select option from my dropdown with duplicate value should appears only once for eg Himanshu and when the Himanshu is selected then o/p should be as

id name
2 Himanshu
4 Himanshu

and when option supriya is selected then my o/p should be as

id name
1 Supriya

i'm trying to get the value in name field

Ooops, didnt make the right query ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.