有可能在mysql中简化多个UNION ALL吗?

时间:2016-10-19 05:45:17

标签: mysql sql union

我有一个总是需要比较4个值的MySQL查询,我需要使用UNION ALL将这些值添加到行中,但是这个代码看起来非常大,我想简化它。 代码如下:

SELECT
    trimestre,

IF ((meta = 0) OR (meta is null) or not(meta REGEXP '[0-9]+'), :meta, meta) as valor
FROM
    (
        SELECT
            trimestre,
            valor AS meta
        FROM
            view_valores_indicadores
        WHERE
            anio = :anio
        AND codigo_indicador = :codigo_indicador
        AND alias_campo = 'meta'
        UNION ALL
            SELECT
                1,
                :meta
            UNION ALL
                SELECT
                    2,
                    :meta
                UNION ALL
                    SELECT
                        3,
                        :meta
                    UNION ALL
                        SELECT
                            4,
                            :meta
                        ORDER BY
                            trimestre ASC
    ) AS T
GROUP BY
    TRIMESTRE

我想知道是否可以简化多个UNION ALL,如

UNION ALL SELECT (row1, row1, row3...)

0 个答案:

没有答案