Thursday, 8 August 2013

Mysql order by two columns if one is grater than timestamp

Mysql order by two columns if one is grater than timestamp

I have this SQL SELECT in PHP:
SELECT * FROM ads ORDER BY ad_sponsored_date DESC, ad_date DESC
I want to display a list of ads, they are normally sorted by date but if
one is sponsored, then it appears in top. The above does that nicely; but
I need to also test if that ad_sponsored_date is not grater than timestamp
and if so, then only sort by the second one (ad_date) , is it possible?
Something like this but I don't know the correct syntax:
SELECT * FROM ads ORDER BY (ad_sponsored_date DESC IF ad_sponsored_date >
$timestamp, ad_date DESC)

No comments:

Post a Comment