2014-06-23 16 views
0

一切從一個數據庫奧德我有以下的數據庫,這使作業:Mysql的表達,選擇按日期倒序

user_job(JOB_ID,JOB_TITLE,日,月,年)。日,月,年字段包含每項工作的到期日期。日持有值1..31,月份持有價值1月... 12月和2014年... 2040年。

我想從這張表中選擇* by expiration date DESC。任何想法如何寫我的表達?我的意思是我想在截止日期DESC之前找工作。 這是我試過,但沒有工作:

<?php 

    $get_jobs = mysql_query("select * FROM `user_job` order by `day`, `month`, `year` DESC "); 

?> 
+1

將日,月,年合併爲一個'timestamp'或'datetime'列。然後您可以按該列排序。不知道爲什麼你有它像這樣分開... – superphonic

回答

3
select * FROM `user_job` 
order by `year` desc, 
      case when `month` = 'january' then 1 
       when `month` = 'february' then 2 
       when `month` = 'march' then 3 
       when `month` = 'april' then 4 
       when `month` = 'may' then 5 
       when `month` = 'june' then 6 
       when `month` = 'july' then 7 
       when `month` = 'august' then 8 
       when `month` = 'september' then 9 
       when `month` = 'october' then 10 
       when `month` = 'november' then 11 
       when `month` = 'december' then 12 
      end desc, 
     `day` desc 
+0

看起來不錯,但不工作...像缺少某些東西 – user2491321

+0

@ user2491321:是的,我換了一天一個月,現在正確的感謝其他用戶的編輯。 –

+0

好吧......現在就完美了......非常感謝 – user2491321

0

嘗試像

select * FROM `user_job` order by date(concat(`year`,'-',`month` ,'-',`day`)) DESC 
0

東西只要使用這個簡單的查詢。

select * FROM `user_job` order by STR_TO_DATE(concat(`month`,'/',`day`,'/',`year`), '%M/%e/%Y') desc