how to order, then limit, then order?

This is a discussion on how to order, then limit, then order? within the MySQL Database forums, part of the Database Forums category; Hello. I hope this question isn't too silly. I am working on a PHP/ mysql application that has a ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-22-2007
dankelley
 
Posts: n/a
Default how to order, then limit, then order?

Hello. I hope this question isn't too silly. I am working on a PHP/
mysql application that has a list of (scientific research) papers, a
list of keywords, and a list that makes links between the two.

What I'm trying to do is to show just the most popular keywords, and
to show them in alphabetical order.

My approach (illustrated at the end, with a query that workd) is to
order by a count of use, and then to limit the results. But, as I
imagine the clever folks who read this group will see quickly, the
query leaves the keywords ordered by popularity.

Q: is there a way that I can get Mysql to take the results of this
query and then order them alphabetically, or should I be doing that in
PHP? (I assume it would be faster to do it in mysql. And, I may as
well admit it, I am enjoying learning mysql, so I'd like to get this
task done in that domain instead of in PHP.)

Many thanks, to anyone who is kind enough to offer some advice.

Dan.

THE QUERY IN QUESTION --


SELECT Keyword, KeywordID, count( PaperID )
FROM Papers_Keywords
LEFT JOIN Keywords ON ( Keywords.id = Papers_Keywords.KeywordID )
GROUP BY KeywordID
ORDER BY count( Keyword ) DESC
LIMIT 0 , 5;

Reply With Quote
  #2 (permalink)  
Old 07-22-2007
Rik
 
Posts: n/a
Default Re: how to order, then limit, then order?

On Sun, 22 Jul 2007 01:46:11 +0200, dankelley <Dan.Kelley@Dal.Ca> wrote:

> Hello. I hope this question isn't too silly. I am working on a PHP/
> mysql application that has a list of (scientific research) papers, a
> list of keywords, and a list that makes links between the two.
>
> What I'm trying to do is to show just the most popular keywords, and
> to show them in alphabetical order.
>
> My approach (illustrated at the end, with a query that workd) is to
> order by a count of use, and then to limit the results. But, as I
> imagine the clever folks who read this group will see quickly, the
> query leaves the keywords ordered by popularity.
>
> Q: is there a way that I can get Mysql to take the results of this
> query and then order them alphabetically, or should I be doing that in
> PHP? (I assume it would be faster to do it in mysql. And, I may as
> well admit it, I am enjoying learning mysql, so I'd like to get this
> task done in that domain instead of in PHP.)
>
> Many thanks, to anyone who is kind enough to offer some advice.
>
> Dan.
>
> THE QUERY IN QUESTION --



SELECT * FROM (
SELECT Keyword, KeywordID, count( PaperID )
FROM Papers_Keywords
LEFT JOIN Keywords ON ( Keywords.id = Papers_Keywords.KeywordID )
GROUP BY KeywordID
ORDER BY count( Keyword ) DESC
LIMIT 5;
) ORDDER BY Keyword

--
Rik Wasmus
Reply With Quote
  #3 (permalink)  
Old 07-22-2007
dankelley
 
Posts: n/a
Default Re: how to order, then limit, then order?

Thanks very much, Rik. It turns out that I also needed an "As"
clause. So, for anyone who has come upon this thread because they
were having a similar problem, the full, working answer is as follows.

SELECT *
FROM (

SELECT Keyword, KeywordID, count( PaperID )
FROM Papers_Keywords
LEFT JOIN Keywords ON ( Keywords.id = Papers_Keywords.KeywordID )
GROUP BY KeywordID
ORDER BY count( Keyword ) DESC
LIMIT 0 , 5
) AS TopKeywords
ORDER BY Keyword
LIMIT 0 , 30;

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:49 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0