Jump to content

MySQL query, with DISTINCT issue


Hyperjase

Recommended Posts

I've got a bit of a problem, I think I could do this with two queries but I would prefer to do it all as one, basically I have this code:

 

SELECT DISTINCT album FROM samdb.songlist WHERE album !='' GROUP BY album ORDER BY date_added DESC LIMIT 5

 

So that pulls all the unique album titles (except where it's empty), what I need to do now is pull the artist from the same table .... but with the DISTINCT ... it only works on one column (album) as far as I know ... what's the best way of pulling the artists for the unique album too?

 

Thanks,

 

Jason

Link to comment
https://forums.phpfreaks.com/topic/118494-mysql-query-with-distinct-issue/
Share on other sites

Thanks for that .. I understand how that works now, but have realised it won't work anyway.... on some albums there are multiple artists, which then shows each time for the album, whereas I just want it to show as "Various" ... but nevermind, it's fine the way it is.

 

I do have one other quick question though.

 

I want to be able to set the number shown using a drop down box, for the life of my I can't figure out how to use a $_POST in the MySQL query .. how do I do that?  Here's what I had...

 

$result = mysql_query("SELECT DISTINCT album FROM samdb.songlist WHERE album !='' GROUP BY album ORDER BY date_added DESC LIMIT '".$_POST['noalb']"'")

 

I think I have either too many "s or something odd ... but it doesn't want to know!

 

Thanks for your help,

 

Jason

safe way

$lim = intval($_POST['noalb']);  // ensure it's a number
$result = mysql_query("SELECT DISTINCT album 
          FROM samdb.songlist 
          WHERE album !='' 
          ORDER BY date_added DESC 
          LIMIT $lim ");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.