SQL查询按时间排序asc不起作用

时间:2015-05-20 06:22:22

标签: sql time sql-order-by

SELECT time,
       null as postid,
       designid,
       custid 
FROM approveddesign 
WHERE designerid='abc@gmail.com' 
UNION 
SELECT time,
       postid,
       null as designid,
       null as custid 
FROM applycontest
WHERE approval='approved' and designerid='abc@gmail.com' 
ORDER BY 'time' ASC

按时间排序不起作用,因为时间列仅包含数值。

3 个答案:

答案 0 :(得分:1)

删除ORDER BY子句中col名称周围的引号。

[...] ORDER BY time ASC

答案 1 :(得分:1)

'time'更改为timeORDER BY time ASC

答案 2 :(得分:0)

  

可以将排序列指定为名称或列别名,或者表示选择列表中名称或别名位置的非负整数。

如果您真的想订购所有结果,请使用:

SELECT *
FROM (
    SELECT time,
           null as postid,
           designid,
           custid 
    FROM approveddesign 
    WHERE designerid='abc@gmail.com' 
    UNION ALL 
    SELECT time,
           postid,
           null as designid,
           null as custid 
    FROM applycontest
    WHERE approval='approved' AND designerid='abc@gmail.com' 
    ) t
ORDER BY 1

我还将UNION更改为UNION ALL以获得更好的效果,但不会删除重复的行,如果您想删除这些重复的行,我建议您添加DISTINCTSELECT之后:

SELECT DISTINCT *
FROM (
    ...
    ) t
ORDER BY time