这个MySQL查询的语法有什么问题?

时间:2012-10-29 23:51:52

标签: mysql

我有一个相当简单的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服务器版本,以便使用正确的语法   在'UNION ALL SELECT id,start_date AS date,'contract start'AS附近   在第1行输入FROM bus_contr'

我确信这是显而易见的,但我无法弄明白。谁能发现我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

您需要删除前导)

答案 1 :(得分: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