Dear All,

I'm facing a problem when using DISTINCT in mysql. Please refer my database design below.

id  lang    album   singer  title   family  writer  song    hits

In my db i want distinct title and the id from the db.
I'm trying the following code. But it only fetching the results but not the id. If i try its again displaying all the records.

Now i'm trying to display the results using query

SELECT DISTINCT `title` FROM `songs` WHERE `lang` = 'Telugu'

If i try this

SELECT id, DISTINCT title FROM songs WHERE lang = 'Telugu'

I'm getting nothing from the db.

Please help me which this. Thanks in advance.

Recommended Answers

SELECT DISTINCT id, title FROM songs WHERE lang = 'Telugu'

should work. If not, you can always use GROUP BY.

Jump to Post

All 6 Replies

Re: Problem facing on DISTINCT in mysql using PHP 80 80
SELECT DISTINCT id, title FROM songs WHERE lang = 'Telugu'

should work. If not, you can always use GROUP BY.

commented: Helped me to learn new thing in mysql i.e group by +2
Re: Problem facing on DISTINCT in mysql using PHP 80 80

Its will not work i guess because id is the primary key. So that it always unique. Can u elabrate the GROUP BY for my example if possible ?

Re: Problem facing on DISTINCT in mysql using PHP 80 80

Can you elaborate on exactly what information you are trying to retrieve? If you do not include the id in your query, you will get a distinct list of titles. However, you want to know the ID of each distinct title. So, if you add in the ID column in your query, you will get that information.

I am not clear on what you are trying to get in your results.

Re: Problem facing on DISTINCT in mysql using PHP 80 80

Dear JorgeM,

I'm trying to get the id of the distinct elements. if i say
SELECT id, DISTINCT title FROM songs WHERE lang = 'Telugu'
Its showing all the records from the table. I want only id's of the perticular distinct titles. Please help me

Re: Problem facing on DISTINCT in mysql using PHP 80 80

Show some data, and what you want as output, to demonstrate.

Re: Problem facing on DISTINCT in mysql using PHP 80 80

how about select id from songs where lang='Telugu' group by title

commented: Worked gr8 :) thnx alooooooooooooot +2
Be a part of the DaniWeb community

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