Jump to content

[SOLVED] Order By Two Fields Simeoultaneously


br0ken

Recommended Posts

I have a table of addresses. In this table there are two fields, dtCreated and dtModified.

 

dtCreated stores a timestamp of the when the address was first input into the database and dtModified stores a timestamp of the last time it was updated or accessed.

 

There are other fields in the table but these aren't relevant to my problem.

 

Basically, I want to retrieve all addresses and order them with the ones accessed recently at the top. Is there a way to order the query by the two fields at the same time?

 

These two queries DO NOT produce what I want:

 

SELECT * FROM tblAddress ORDER BY dtModified DESC, dtCreated DESC

SELECT * FROM tblAddress ORDER BY dtCreated DESC, dtModified DESC

 

Can any one help?

Link to comment
Share on other sites

Hi

 

Think what you are after is to order by when the field was last modified, or created. Would be easiest if when the record was created the last modified field was set to the created date.

 

Anyway, this this would work:-

 

SELECT IF(dtModified >  dtCreated, dtModified, dtCreated) AS LastTouched FROM tblAddress ORDER BY LastTouched DESC

 

All the best

 

Keith

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.