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?

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

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.