2012-10-29 24 views
0

我有一個非常簡單的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 

運行它不過是給我的語法錯誤:

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

我敢肯定,這是顯而易見的事情,但我不能很圖出來。任何人都可以發現我在這裏做錯了嗎?

回答

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 
+0

的'1'不需要被改變爲'1 = 1'。該表達仍然等同於真實。你寫的這個例子的其餘部分幫助我修復了我的問題。謝謝! – dqhendricks

0

您有領先的)需要刪除。