2013-08-05 99 views
-3

我有這個輸入。在mysql查詢中按名稱,日期,日期排序?

October 21 , 2010 
October 14 , 2007 
October 08 , 2010 
March 19 , 2009 
June 25 , 2009 
June 21 , 2013 
June 21 , 2013 
June 19 , 2006 
June 01 , 2009 
July 26 , 2013 
July 25 , 2012 
July 23 , 2013 
July 19 , 2013 
July 01 , 2009 
January 31 , 2013 
January 23 , 2013 
January 23 , 2011 
January 19 , 2013 
January 01 , 2000 
February 17 , 2011 
February 16 , 1998 
December 25 , 2003 
August 31 , 2013 
August 04 , 2013 
August 03 , 2013 
August 03 , 2013 
August 03 , 2013 
April 26 , 2013 
April 26 , 2013 
April 19 , 2005 

我想訂購,使最新的日期是在頂部..但我似乎無法做到這一點,有人可以提供一個解決方案?

這是我的SQL:

$sql = "SELECT * FROM $tbl_name WHERE Length(Year) > 7 AND `Title` 
LIKE '%" . $q . "%' AND `Genres` LIKE '%" . $genre . "%' AND `Actors` 
LIKE '%" . $actor . "%' AND `Year` <> 'Unknown' 
ORDER BY `Year` DESC LIMIT $start, $limit"; 
+0

所以你提到的日期來自3個不同的列? – DevelopmentIsMyPassion

+0

和你的mysql查詢在哪裏? – Salil

+0

'ORDER BY Year DESC,month DESC,day DESC' –

回答

1

所以,這是2列,對不對?

然後像這樣做:

SELECT 
STR_TO_DATE(CONCAT(month_day_column, ', ', year_column), '%M %d, %Y') AS your_ISO_conform_date 
FROM yourTable 
ORDER BY your_ISO_conform_date DESC 
  • 閱讀更多關於str_to_date()here

  • here是關於格式的一些信息,你必須爲參數提供給str_to_date()

+0

「2005年4月19日」是我db中的一列 – Dean

+0

然後更簡單:'SELECT STR_TO_DATE(your_column,'% M%d,%Y')作爲column_alias FROM yourT ORDER BY column_alias' – fancyPants