Jump to content

Recommended Posts

Hi all

 

I have a database table which has the following fields;

 

department | firstname | lastname | mydatetime

 

I run the following QUERY at the moment

 

SELECT * FROM `users` ORDER BY lastname DESC

 

I do this type of "ORDER BY" to ensure that anyone who has an empty value for `lastname` appears at the bottom of the list and anyone who has a value for `lastname` would appear at the top of the list

 

What I am stuck with is, I need to keep the query the same (ie: anyone who has a empty value for `lastname` is at the bottom), but I want to order the results by `mydatetime` which is a datetime value written like "2011-07-20 10:20:45"

 

So it would keep rows with no value for `lastname` at the bottom, but it would order all the results using the datetime field `mydatetime`

 

Anyone got any ideas?

Link to comment
https://forums.phpfreaks.com/topic/243999-sql-order-by/
Share on other sites

that doesn't seem to sort it into date order, it keeps rows with no value for `lastname` at the bottom of the list (which is what I need), but it doesn't sort it correctly into date order as it returns

 

 

2011-08-05 19:19:25

2011-08-05 19:16:29

2011-08-06 00:41:01

2011-08-05 21:52:17

 

any ideas why?

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/243999-sql-order-by/#findComment-1253016
Share on other sites

edit: ^^ As mentioned.

 

hmm

 

When ordering by more than one column, the second column is only used if the values are identical with the onces in the first column

 

I guess I used the wrong approach for this, let me cook something up for you, otherwise maybe we can get kickstart or someone else in here to help.

 

try this

 

SELECT * FROM `table_name` ORDER BY `field1`, `field2` DESC

 

or try it with ASC

Link to comment
https://forums.phpfreaks.com/topic/243999-sql-order-by/#findComment-1253021
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.