简体   繁体   中英

What is wrong with the syntax for this MySQL query?

I have a fairly simple MySQL query:

(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

Running it however is giving me the syntax error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION ALL SELECT id, start_date AS date, 'contract start' AS type FROM bus_contr' at line 1

I'm sure it is something obvious, but I can't quite figure it out. Can anyone spot what I'm doing wrong here?

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

remove WHERE 1 or change it to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM