我需要MySQL日期時間字段的條件ORDER BY語句。我有,我想通過以下方式訂購職位表:所有以後的文章中應當責令ASC,並下令DESC所有歷史帖子。例如:有條件的ORDER BY ASC/DESC日期列
post_status post_date post_title
=========== ========= ==========
future 2012-10-01 Title 1
future 2012-12-01 Title 2
publish 2012-05-01 Title 3
publish 2012-01-01 Title 4
我需要類似下面的SQL東西...
SELECT post_status, post_date, post_title FROM wp_posts
WHERE post_status IN ('future', 'publish')
ORDER BY post_status ASC,
CASE post_status
WHEN 'future' THEN 'post_date ASC'
ELSE 'post_date DESC'
END;
如何做到這一點的任何提示? 謝謝!
這是一個更好的解決方案,因爲它僅需要表的單次掃描。 –
太棒了!也似乎在伎倆!謝謝! – Mike