0

我有一个相当简单的 MySQL 查询:

(SELECT
    id
    , creation_date AS date
    , 'order' AS type
    FROM bus_orders
    WHERE 1
UNION ALL SELECT
    id
    , start_date AS date
    , 'contract start' AS type
    FROM bus_contracts
    WHERE 1
UNION ALL SELECT
    id
    , end_date AS date
    , 'contract end' AS type
    FROM bus_contracts
    WHERE 1
) ORDER BY date DESC LIMIT 5

但是运行它会给我语法错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'UNION ALL SELECT id, start_date AS date, 'contract start' AS type FROM bus_contr' 附近使用正确的语法

我敢肯定这是显而易见的,但我无法完全弄清楚。谁能发现我在这里做错了什么?

4

2 回答 2

0

您有一个)需要删除的前导。

于 2012-10-29T23:53:05.103 回答
0

删除WHERE 1或更改为WHERE 1=1

SELECT *
FROM
    (
        SELECT  id , creation_date AS date , 'order' AS type
        FROM bus_orders
        UNION ALL 
        SELECT id , start_date AS date , 'contract start' AS type
        FROM bus_contracts
        UNION ALL 
        SELECT id , end_date AS date , 'contract end' AS type
        FROM bus_contracts
    ) x
ORDER BY date DESC 
LIMIT 5
于 2012-10-29T23:53:14.153 回答