Forum Moderators: coopster

Message Too Old, No Replies

how to Query from one column then sort using another column

         

hwange

11:53 pm on Jun 9, 2005 (gmt 0)

10+ Year Member



Hi all I am new to php and mysql but have come a long way in a short time.

Little things like this seem to get me stuck although I know there is a simple answer I'm missing somewhere.

I am trying to get the last 100 albums I entered into my database using the album_id (Highest being the latest entry) then sort them alphabetically using the album_title column.

I can get the latest 100 easily but cannot figure out how to also sort the results by the album_title before I display them.

This is what I have so far which gets what I want but not in alphabetical order.

<?php

$sqlquery = mysql_query("SELECT * FROM albums ORDER BY album_id desc limit 100", $db);

while ($result = mysql_fetch_row($sqlquery))
{
echo "<a href='music_info.php?album_id=$result[0]'><span style='text-decoration: none'>$result[1]</span><br></a>";
}

?>

I have been trying to add a second ORDER BY to the query like so.

$sqlquery = mysql_query("SELECT * FROM albums ORDER BY album_id desc limit 100 ORDER BY album_title", $db);

but this obviously isn't the way I should be doing it.

HELP PLEASE!

hwange

1:01 am on Jun 10, 2005 (gmt 0)

10+ Year Member



Alternatively, I currently have 3839 albums therefore the highest album_id is 3839.

This query does exactly what I wanted.

$sqlquery = mysql_query("SELECT * FROM albums where album_id >= 3739 ORDER BY album_title", $db);

As the amount of rows gets higher when I add to the database I will just have to count the rows then subtract 100 from the result and use that in the query.

I know I've seen info about counting rows somewhere and am going in search of the answer but please advise if possible incase I get lost along the way

Thanks in advance

hwange

1:15 am on Jun 10, 2005 (gmt 0)

10+ Year Member



OK I'm happy to say I figured it out

$getcount = mysql_query("SELECT album_id FROM albums");
$num_rows = mysql_num_rows($getcount);

$last100 = $num_rows-100;

$sqlquery = mysql_query("SELECT * FROM albums where album_id > $last100 ORDER BY album_title", $db);

jatar_k

3:44 pm on Jun 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld hwange,

geez, too fast, didn't even get a shot at it ;)

glad you got it sorted

hwange

4:27 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



Thank you jatar_k

Glad to be here.

Sometimes it seems we can sit for hours trying to work something out then when we write out the question on a forum and read it back it somehow becomes a little more clear.

But the answers were mostly found by browsing through the rest of the forum and picking up bits here and there.

Great site.