Web Hosting Talk







View Full Version : pull 10, but different


ti_nhatrang
07-06-2007, 12:19 PM
Hi guys,

I want to pull 10 genre from the database, but I want the genre to be different from each other, how can I accomplish this? This is what I got so far:

$genre = mysql_query("SELECT genre,picture FROM songlist WHERE songtype='S' AND genre IS NOT NULL LIMIT 10") or die(mysql_error());

Thanks in advance...

mwatkins
07-06-2007, 12:35 PM
http://dev.mysql.com/doc/refman/5.0/en/select.html

Read about the DISTINCT clause. Not knowing anything about your data, I can't tell if there is a 1:1 relationship between "picture" and genre. If there isn't, simply adding DISTINCT to your query isn't going to work, as you'll get multple permutations of genre because the tuple being returned (genre, picture) has more DISTINCT permutations. Thus if "picture" is an album cover or band photo, you have an issue:

rock rollingstones.gif
rock u2.gif
rock thehip.gif

However if there is a 1:1 between picture and genre, then it'll be fine:

rock rockpic.gif
r+b randb.gif

If not, the simple solution is to do a separate query for genre alone.

ti_nhatrang
07-06-2007, 12:45 PM
yeah, picture is not the same as genre... i wonder how I can achieve on pulling only different genre...

mwatkins
07-06-2007, 12:58 PM
I already gave you the answer to that question. Remove "picture" or do a separate query for genre alone.

Look at the code. Do you really need "picture" returned in your "$genre" result?

ti_nhatrang
07-06-2007, 12:59 PM
right, but is there a way we can do it without doing two query?

mwatkins
07-06-2007, 01:07 PM
Think logically and you can answer that question for yourself.

Your database table contains information like:
rock stones.jpg
rock thehip.jpg
r+b sledge.jpg
rock stones.jpg
folk ppm.jpg

SELECT genre, picture from songlist: returns a list of tuples:

(rock, stones.jpg)
(rock, thehip.jpg)
(r+b, sledge.jpg)
(rock, stones.jpg)
(folk, ppm.jpg)

select DISTINCT genre, picture from songlist; returns the following set of tuples:

(rock, stones.jpg)
(rock, thehip.jpg)
(r+b, sledge.jpg)
(folk, ppm.jpg)

Given the query you are doing, no, there is no way to have the SQL engine return only:

(rock)
(folk)
(r+b)

You could filter the SQL result using code. However then you've got to take the LIMIT off your original query because you'll be filtering an incomplete list of genres.

All of this leaves you with:
SELECT DISTINCT genre FROM songlist;
SELECT DISTINCT genre FROM songlist LIMIT 10;

mwatkins
07-06-2007, 02:22 PM
So I ask again, the obvious question: Do you really need "picture" in the result? Doesn't seem like you would, given that "picture" isn't related to genre.

sasha
07-06-2007, 07:29 PM
SELECT genre,picture FROM songlist
WHERE songtype='S' AND genre IS NOT NULL
GROUP BY genre
LIMIT 10

That could prove to be rather inefficient for large table.

ti_nhatrang
07-07-2007, 02:09 AM
So I ask again, the obvious question: Do you really need "picture" in the result? Doesn't seem like you would, given that "picture" isn't related to genre.

Thank you for your help and support, after many hours of researching and reading, the best way is to pull it out individually like you said and then just Distinct it...

Thank you once again.