Jump to content

problem with GROUP BY and ORDER BY


gdfhghjdfghgfhf

Recommended Posts

i usually use this query to display the last 10 entries from a sql table:

 

$query = "SELECT search_time, search_keywords FROM phpbb_popsearch ORDER BY search_time DESC limit 35"; 

 

as you can see it will order the results by "search_time" so the latest entries are listed first

 

normally the result would be something like this:

new entry

duplicate

another entry

bla bla

348237

543543

duplicate

 

but now i just want to include a "GROUP BY" in this sql request

$query = "SELECT search_time, search_keywords FROM phpbb_popsearch ORDER BY search_time DESC limit 35"; 

 

but with the GROUP BY my sql query isn't ordered correctly anymore.

 

the result would be something like this:

new entry

another entry

bla bla

348237

543543

duplicate

notice the "duplicate" entry was grouped together, but it's not in the second first position anymore ???

 

i was expecting a result like this:

new entry

duplicate

another entry

bla bla

348237

543543

Since entry called "duplicate" is the second last added entry....

 

i hope you understand what i mean ^^

 

thanks for help

Link to comment
Share on other sites

I'm not sure I understand...

Try typing

echo $query;

and cut that code into phpMyAdmin. Occasionally you get weird results if you have variables in there.

 

Otherwise, encapsulate your field names... it helps:

$query = "SELECT `search_time`, `search_keywords` FROM `phpbb_popsearch` ORDER BY `phpbb_popsearch `.`search_time` DESC limit 35";

Link to comment
Share on other sites

Damn, sorry

 

here is the query with the GROUP BY request

 

$query = "SELECT search_time, search_keywords FROM phpbb_popsearch GROUP BY search_keywords ORDER BY search_time DESC limit 35";

 

So it will group together entries with the same name, but it will lose its original position ! (i.e. first entries will not be first anymore once its grouped)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.