NYCPHP Meetup

NYPHP.org

[nycphp-talk] SQL - Sorting on multiple columns

Eric K. kigathi at gmail.com
Fri Sep 30 08:32:19 EDT 2005


Quick SQL-related question that seems deceptively simple but that I
can't seem to solve:

I have a database with authors and books they've written-looks very similar to:
http://www.onlamp.com/pub/a/php/2004/01/29/php_foundations.html

I have a simple query that returns a resultset of the names of the top
25 authors and the books they've published thus:

SELECT authors.name, COUNT(books.title) AS books
FROM authors,books
WHERE books.author_id = authors.author_id
GROUP BY name
ORDER BY books
LIMIT 0,25

Pretty straightforward right? Actually no, because I'd like to have
the result set returned in order of author name but because of the top
25 stipulation (ORDER by books LIMIT 0,25) I can't.

Is there any way to get the top 25 resultset sorted by author name
with a straightforward SQL query (unfortunately MySQL 4.0 doesn't like
subselects). Unfortunately I can't sort the resultset in PHP.



More information about the talk mailing list