br0ken Posted March 8, 2009 Share Posted March 8, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/148534-solved-order-by-two-fields-simeoultaneously/ Share on other sites More sharing options...
fenway Posted March 9, 2009 Share Posted March 9, 2009 "simultaneously" doesn't make any sense... what do you actually want? Quote Link to comment https://forums.phpfreaks.com/topic/148534-solved-order-by-two-fields-simeoultaneously/#findComment-780147 Share on other sites More sharing options...
kickstart Posted March 9, 2009 Share Posted March 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148534-solved-order-by-two-fields-simeoultaneously/#findComment-780209 Share on other sites More sharing options...
br0ken Posted March 9, 2009 Author Share Posted March 9, 2009 Thanks kickstart, I think that's what I'm looking for! Quote Link to comment https://forums.phpfreaks.com/topic/148534-solved-order-by-two-fields-simeoultaneously/#findComment-780344 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.