2017-04-01 46 views
-1

我有表:PHP MySQL的順序由第一列,如果第二個空

id | post_date | bumped_post_date 
1 | date  |  NULL 
2 | date  |  date2 

好吧,我知道如何post_date訂購,但我wan't通過post_date只訂購時bumped_post_dateNULL

bumped_post_date不爲NULL時,則按順序排序,而不是按post_date排序(但如果爲NULL,則按post_date排序)。謝謝你,並且爲我的壞英語感到抱歉

+0

你或許可以使用MySQL的CASE https://dev.mysql.com/doc/refman/5.7/en/case.html –

+0

或帶有AND和OR的子句的WHERE子句。 https://dev.mysql.com/doc/refman/5.7/en/subqueries.html –

回答

2

select * from my_table 
order by case when bumped_post_date is null 
      then post_date else bumped_post_date end 
1

可以在ORDER BY使用CASE

select * 
from your_table 
order by case 
     when bumped_post_date is not null 
      then bumped_post_date 
     else post_date 
     end; 
+0

非常感謝! – Evaldas

2
ORDER BY COALESCE(bumped_post_date, post_date) 

COALESCE()函數返回第一個非空參數,你可以使用的情況。

相關問題