NYCPHP Meetup

NYPHP.org

[nycphp-talk] SQL - Sorting on multiple columns

Ophir Prusak prusak at gmail.com
Fri Sep 30 10:52:10 EDT 2005


If you want the top 25 authors (in terms of books) sorted by author
name, you'll need a subselect. Try mysql 4.1 :)

or insert into a temp table (memory based) and query from there.

Also - why can't u sort it in php ?


On 9/30/05, Eric K. <kigathi at gmail.com> wrote:
> 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.
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org
>



More information about the talk mailing list