PDA

View Full Version : Sorting after getting MYSQL Results


zimmer3
1-4-05, 04:06 PM
Hello,

I have a database of Books. I currently use a SQL to get the 4 newest additions, and then another SQL to list in alphabetic order by title All books.

My problem is that the 4 newest books also show up in the alpha list. I would like to do away with the second SQL statement, and make my alpha list from the entries remaining from the first SQL statement.

How can I do this? How do I sort the remaining results to alpha?

Thanks !

zimmer3
1-5-05, 11:14 AM
No one's got any ideas?

mitchind
1-5-05, 12:14 PM
This would work fine as a subquery (not available until Powweb upgrades to MySQL 4.1)

But since you have to run the two queries anyways, it kind of defeats the purpose. You might as well read the results of the first query into a comma-delimited string - then use that to exclude titles from the second query.

Don't know how much PHP you have under your belt, but maybe a little pseudo-code will help for now.

$query1 = "SELECT titleID, title from titles ORDER BY titleID DESC LIMIT 0,3";

Loop to build comma-delimited list of titleID from $query1 results ...
$lasttitles = "12,11,10,9"

$query2 = "SELECT titleID, title from titles WHERE titleID NOT IN (" . $lasttitles . ") ORDER BY title";

zimmer3
1-5-05, 03:47 PM
>> NOT IN

That did the trick, never heard of that one. Thanks !