如何正确订购“datetime”列?

时间:2011-09-24 07:40:20

标签: mysql

我有一个专栏,时间是'DATETIME'。我想按时间轴的顺序输出它,但是当我使用ORDER BY create_time DESC时,'2011-09-10 16:16:04'落后'2011-07-16 03:20:01'。

似乎它正在将datatime列视为字符串。

如何在不更改列类型的情况下处理此问题?

更新:

这是我的查询的一部分:

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" }

2 个答案:

答案 0 :(得分:2)

您似乎将数据包含在错误的列中。在您的输出示例中,create_time对于所有这些都是3,因此订单未定义。仔细检查数据在右栏?

答案 1 :(得分:0)

尝试使用

ORDER BY CONVERT (create_time, DATETIME) DESC

但要注意这会花费一些性能......改变列类型是恕我直言更好的选择!