2011-09-24 82 views
0

我有一個專欄,時間是'DATETIME'。我想按時間順序輸出,但是當我使用ORDER BY create_time DESC時,'2011-09-10 16:16:04'在'2011-07-16 03:20:01'後面。
如何使'datetime'列正確排序?

似乎它將datatime列當作字符串處理。

如何在不更改列類型的情況下處理此問題?

UPDATE:

這是我的查詢的一部分:輸出

SELECT 'bookcomment' AS type 
      ,b.book_id 
      ,b.name 
      ,c.book_id 
      ,c.author_id 
      ,c.content 
      ,c.create_time as create_time 
      ,u.id 
      ,u.name 
      FROM tbl_book AS b, 
         tbl_book_comment AS c, 
         tbl_user AS u 
      WHERE u.id=c.author_id in (1,2) AND b.book_id=c.book_id 

UNION ALL 

SELECT 'bookreview' AS type 
      ,b.book_id 
      ,b.name 
      ,re.book_id 
      ,re.author_id 
      ,re.content 
      ,re.create_time as create_time 
      ,u.id 
      ,u.name 
      FROM tbl_book AS b, 
         tbl_book_review AS re, 
         tbl_user AS u 
      WHERE u.id=re.author_id in (1,2) AND b.book_id=re.book_id 

UNION ALL 
... 
      ORDER BY create_time DESC 

而且部分:

array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "Whatever" ["author_id"]=> string(4) "test" ["content"]=> string(19) "2011-07-16 03:20:01" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" } 
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(13) "sdfwefwaefwea" ["content"]=> string(19) "2011-05-11 03:33:33" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" } 
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test0" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" } 
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test1" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" } 
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test2" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" } 
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test3" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" } 
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test4" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" } 

+0

您爲什麼不想將列類型更改爲日期時間?這顯然是數據。 –

+0

我假設你有datetime列的數據類型爲datetime? –

+3

即使將該列視爲字符串,順序也是相同的。 「2011-09」>「2011-07」 –

回答

2

您似乎有錯誤的列中的數據。在您的輸出示例中,create_time對於它們都是3,所以訂單未定義。仔細檢查數據是否在正確的列?

+0

現在我發現每一列都不對應其數據...怎麼了! – LotusH

+0

發生在我身上的一次......在800萬行的桌子上,不下於! –

0

嘗試使用

ORDER BY CONVERT (create_time, DATETIME) DESC 

但請注意,這會降低性能......更改列類型是恕我直言,更好的選擇!

+0

它已經是一個日期時間列。 – Inca

+0

@Inca如果你檢查我的答案的時間,那麼你可以清楚地看到當時關於列類型的信息是不可用的(它後來被OP添加了!)。 – Yahia