联合使用订单

时间:2012-10-22 08:39:50

标签: mysql

我有一个查询

select username,amount from employee
union all
select '' as username,sum(amount) from employee
order by cast(username as decimal)

用户名从1000开始

当我使用此查询时,它始终显示最小用户名

的最高用户名

我想将最小的用户名显示为最高用户名

我为此做了什么?

1 个答案:

答案 0 :(得分:1)

通过包装在子查询中来尝试它,

SELECT *
FROM
    (
        SELECT username, amount from employee
        UNION ALL
        SELECT '' as username, sum(amount) from employee
    ) x
ORDER BY (CASE WHEN username = '' THEN 1 ELSE 0 END) ASC, 
          CAST(username AS SIGNED) ASC