PL \ SQL在UNION查询中使用ORDER BY

时间:2016-04-05 09:06:15

标签: plsql

我有以下查询:

    SELECT
    USERS.*,
    ROWNUM AS RANK ,
    4101   AS TOTAL
FROM
   ( (
        SELECT
            *
        FROM
            USER_LIST
        WHERE
            USER_LIST.USR_ID = 1)
        UNION(
        SELECT
            *
        FROM
            USER_LIST)
    ) USERS

哪种方法完全正常,我想通过'ORDER BY'对结果进行排序。 我已尝试在最后一个括号之前(在USERS之前)放置'ORDER BY',但是我收到错误,请告知我如何在此查询中使用'ORDER BY'。

感谢。

1 个答案:

答案 0 :(得分:2)

您需要在别名之后定义order by

SELECT USERS.*,
         ROWNUM  AS RANK,
         4101    AS TOTAL
FROM ((SELECT * FROM USER_LIST WHERE USER_LIST.USR_ID = 1) UNION (SELECT * FROM USER_LIST)) USERS ORDER BY ...