2017-05-26 28 views
0

我一直在看這幾個小時,我看不出錯誤MySQL的多個UNION:別名爲

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 't2 UNION (SELECT a.item_id ' at line 34'

我試過括號和別名的不同變化,我試着用SELECT DISTINCTUNION ALL ,我已經驗證了?參數匹配的數量......我錯過了什麼?

SELECT COUNT(*) 
FROM (SELECT a.item_id 
     FROM  catalog_items AS a 
     JOIN  catalog_franchises AS b ON a.game_id = b.franchise_id 
     JOIN  catalog_franchises AS c ON a.manufacturer_id = c.franchise_id 
     LEFT JOIN catalog_franchises AS d ON a.exclusive_id = d.franchise_id 
     JOIN  catalog_item_categories AS e ON a.item_id = e.item_id 
       AND e.valid = TRUE 
     JOIN  catalog_categories AS f ON e.cat_id = f.cat_id AND f.parent_id = ? 
       AND f.cat_id IN (?,?,?) AND f.valid = TRUE 
     WHERE  d.franchise_id = ? 
       AND a.valid = TRUE 
       AND b.valid = TRUE 
       AND c.valid = TRUE 
       AND (d.valid = TRUE OR d.title IS NULL) 
       AND a.region_id = ? 
       AND a.quantity IS NOT NULL) t1 
     UNION 
    (SELECT a.item_id 
     FROM  catalog_items AS a 
     JOIN  catalog_franchises AS b ON a.game_id = b.franchise_id 
     JOIN  catalog_franchises AS c ON a.manufacturer_id = c.franchise_id 
     LEFT JOIN catalog_franchises AS d ON a.exclusive_id = d.franchise_id 
     JOIN  catalog_item_categories AS e ON a.item_id = e.item_id 
       AND e.valid = TRUE 
     JOIN  catalog_categories AS f ON e.cat_id = f.cat_id AND f.parent_id = ? 
       AND f.cat_id IN (?,?) AND f.valid = TRUE 
     WHERE  d.franchise_id = ? 
       AND a.valid = TRUE 
       AND b.valid = TRUE 
       AND c.valid = TRUE 
       AND (d.valid = TRUE OR d.title IS NULL) 
       AND a.region_id = ? 
       AND a.quantity IS NOT NULL) t2 
     UNION 
    (SELECT a.item_id 
     FROM  catalog_items AS a 
     JOIN  catalog_franchises AS b ON a.game_id = b.franchise_id 
     JOIN  catalog_franchises AS c ON a.manufacturer_id = c.franchise_id 
     LEFT JOIN catalog_franchises AS d ON a.exclusive_id = d.franchise_id 
     JOIN  catalog_editions AS e ON a.edition_id = e.edition_id AND e.edition_id IN (?,?,?) AND e.valid = TRUE 
     WHERE  d.franchise_id = ? 
       AND a.valid = TRUE 
       AND b.valid = TRUE 
       AND c.valid = TRUE 
       AND (d.valid = TRUE OR d.title IS NULL) 
       AND a.region_id = ? 
       AND a.quantity IS NOT NULL) t3 
+0

你不需要把別名時UNION多條語句... – maSTAShuFu

回答

1

這裏是它應該是什麼樣子

select * from (
select .. 
union 
select... 
) as x -- you need alias here 

你做了什麼錯的是

select ... from 
(
select ... 
) as t1 
union 
select ... -- then this prompted an error because the derived table just ended at the ')', anything after the 't1' is irrelevant 
+0

謝謝,我一旦它讓我接受這個答案。 –