Jump to content

mysql query order by multiple columns


shortysbest

Recommended Posts

I am loading notifications from a database table called "notifications" and I am having a little trouble getting them to order in the correct way.

 

my query I'm using right now:

 

$query = mysql_query("SELECT B.* FROM (SELECT A.* FROM notifications A WHERE A.user_id='$session' AND A.from_id!='$session' ORDER BY A.id ASC ) AS B ORDER BY B.state ASC LIMIT 7");

 

this works well as far as showing the unread notifications on top, then the read notifications below, however it's not ordering the two sets by ID (which the id auto increments so the higher id number is the newest) from newest on top to the oldest on bottom, still keeping them separated by the unread on top, read on bottom (column name is state for showing whether they're read or not).

The order the notifications are displaying by their ID is:

 

3

5

2

4

1

 

when it should be:

 

5

3

2

4

1

Link to comment
https://forums.phpfreaks.com/topic/231732-mysql-query-order-by-multiple-columns/
Share on other sites

This is a short non-descript answer, but I'm not the best with it either. But look into the "GROUP BY" reference for mySQL unfortunately all in all in the end of the output it comes down to a specific column to ORDER BY, no matter how you spin it (i think)

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.