MySQL将“ORDER BY”子句应用于UNION查询

时间:2014-12-24 02:08:43

标签: mysql sql database

ALTER PROCEDURE [dbo].[SP_GET_DDLPage]
AS
BEGIN
    SELECT NULL AS 'ID', '--None--' AS 'DSC'
    UNION
    SELECT MSP_NM AS 'ID', MSP_TITLE AS 'DSC'
    FROM MST_PAGE
    WHERE MSP_STS = ACTIVE ORDER BY MSP_NM
END

我收到错误说:

  

MSP_NM是无效列

如何修复此查询并按MSP_NM排序?

1 个答案:

答案 0 :(得分:0)

我认为最好使用子查询:

SELECT *
FROM (SELECT NULL AS ID, '--None--' AS DSC
      UNION ALL
      SELECT MSP_NM AS ID, MSP_TITLE AS DSC
      FROM MST_PAGE
      WHERE MSP_STS = 'ACTIVE'
     ) t
ORDER BY ID;

你需要'ACTIVE'周围的引号,你可以在没有子查询的情况下运行查询 - 我只是认为子查询澄清了意图。