Jump to content

reorder sql query


gdfhghjdfghgfhf

Recommended Posts

ok so i have this very simple code:

$query = "SELECT search_keywords, COUNT(search_keywords) AS counter FROM phpbb_popsearch GROUP BY search_keywords ORDER BY counter DESC limit 5"; 
$res = mysql_query($query) or die(mysql_error());   

while($row = mysql_fetch_array($res)) { 
$term = $row["search_keywords"];
echo $term;
}

 

phpbb_popsearch is a table where i put the last search terms used in my forum search engine

 

this code will group together the same terms and list the 5 most popular terms

 

my problem is that the results will be ordered by "counter" which is the number of time a term has been found.

 

I want to get the 5 most popular results AND THEN, AFTER, list the results alphabetically

 

if i sort the results alphabetically right in the sql query (replacing "ORDER BY counter" by "ORDER BY search_keywords") it will not work correctly because it will not get the 5 most popular terms but just the 5 first terms, alphabetically sorting them and ignoring the number of times the term has been found

 

thanks for help !!

Link to comment
https://forums.phpfreaks.com/topic/185442-reorder-sql-query/
Share on other sites

ORDER BY counter DESC, search_keywords ASC

 

[edit]

 

Nope... it will not work either...

 

Try like this

 

SELECT * FROM (SELECT search_keywords, COUNT(search_keywords) AS counter FROM phpbb_popsearch GROUP BY search_keywords ORDER BY counter DESC limit 5) AS sq ORDER BY sq.search_keywords

Link to comment
https://forums.phpfreaks.com/topic/185442-reorder-sql-query/#findComment-979023
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.