Jump to content

[SOLVED] Order results by one field, then by another.


pocobueno1388

Recommended Posts

I'm creating an online users script. What I'm trying to do is display moderators at the top of the list, and then all the others members after them, which I have working fine. The problem is that it orders the moderators from highest ID to lowest. I would like it to go lowest to highest.

 

I have a users table, and a moderators table. I don't think I need to post the structure to the tables as it should be clear when you see the query.

 

SELECT
   u.userID, u.username, m.userID as modID
FROM 
   users u
LEFT JOIN 
   moderators m
ON 
   m.userID = u.userID
WHERE 
   last_active > (NOW() - INTERVAL 5 MINUTE)
ORDER BY 
   modID DESC, u.userID

 

To get the moderators to appear at the top, I have no choice but to do "ORDER BY modID DESC". If I put ASC instead, they appear at the bottom.

 

I know I could easily do this with two separate queries, but if it's possible with one, that would be better. Any help is greatly appreciated, thanks.

 

Link to comment
Share on other sites

Okay, the null values are now changing to -1, but it's still not going in the correct order.

 

Updated query:

SELECT 
   u.userID, u.username, IFNULL(m.userID,-1) as modID
FROM 
   users u
LEFT JOIN 
   moderators m
ON 
   m.userID = u.userID
WHERE 
   last_active > (NOW() - INTERVAL 80 MINUTE)
ORDER BY 
   modID ASC, u.userID

 

This displays the moderators in the correct order, but they are at the bottom of the list. If I put DESC instead of ASC I get the same exact result I started with.

 

Did I follow your directions correctly?

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.