2015-05-31 78 views
0

我試圖在一個數據庫UNION從3個表中的記錄選擇,但我有錯誤:從多個表與UNION

#1248 - Every derived table must have its own alias

這是我想查詢:

SELECT * from 
    (SELECT column1, 'table1' from table1 
UNION 
    SELECT column1, 'table2' from table2 
UNION 
    SELECT column1, 'table3' from table3) 
WHERE column1 not like 'abr%' and length(column1) < 8; 

究竟是什麼意思,錯誤以及如何修復它並顯示正確的結果?

+1

添加別名'表1' 作爲t'和派生表 '像'table1''')。 –

回答

2

你需要給派生表的別名像它說:

SELECT * from 
    (SELECT column1, 'table1' from table1 
UNION 
    SELECT column1, 'table2' from table2 
UNION 
    SELECT column1, 'table3' from table3) dtAlias 
WHERE column1 not like 'abr%' and length(column1) < 8; 
+0

第二個條件'和長度(column1)<8'不起作用。它沒有顯示我的結果'<8'符號長度。 '不喜歡'abr%''正在工作。可能是什麼問題? –

+0

嘗試使用'char_length(column1)' –

+0

第二個條件甚至不會返回任何東西如果我知道我有8個符號的值。 –

1

你應該派生表給起了個名字下面

SELECT * from 
    (SELECT column1, 'table1' from table1 
UNION 
    SELECT column1, 'table2' from table2 
UNION 
    SELECT column1, 'table3' from table3) as new_table 
WHERE column1 not like 'abr%' and length(column1) < 8; 
1

錯誤是相當自我explainatory,你只需要命名你使用嵌套選擇和聯合創建的派生表。 它應該是足夠的添加別名來解決我們的錯誤,這樣的事情:

SELECT * from 
    (SELECT column1, 'table1' from table1 
    UNION 
    SELECT column1, 'table2' from table2 
    UNION 
    SELECT column1, 'table3' from table3) as youralias 
    WHERE column1 not like 'abr%' and length(column1) < 8; 
1

您應該添加別名到您在使用子查詢的表。下面是正確的版本:

USE test2; 

SELECT * from 
    (SELECT column1, 'table1' from table1 as t1 
UNION 
    SELECT column1, 'table2' from table2 as t2 
UNION 
    SELECT column1, 'table3' from table3) as t3 
WHERE column1 not like 'abr%' and length(column1) < 8; 
3

你必須在內部選擇或派生表的別名添加您的列,也爲自己,是這樣的:

SELECT * 
FROM 
    (SELECT column1, 'table1' as t from table1 
    UNION ALL 
    SELECT column1, 'table2' as t from table2 
    UNION ALL 
    SELECT column1, 'table3' as t from table3 
    ) As dt 
WHERE 
    column1 NOT LIKE 'abr%' 
    AND 
    length(column1) < 8; 

編輯:
我將UNION更改爲UNION ALL作爲性能問題,並且不會有任何重複;)。

2

正如其他人所指出的那樣,問題很簡單 - 在最後一個括號後需要一個表別名。您還需要第二列的列別名。

由於性能原因,更重要的是您應該使用UNION ALLUNION即被刪除重複的開銷,那就是在這種情況下,完全沒有必要:

SELECT t.* 
FROM (SELECT column1, 'table1' as which from table1 
     UNION ALL 
     SELECT column1, 'table2' from table2 
     UNION ALL 
     SELECT column1, 'table3' from table3 
    ) t 
WHERE column1 not like 'abr%' and length(column1) < 8; 

對於MySQL的性能,我想你想的WHERE從句移至子查詢column1創建索引所有三個表:

SELECT t.* 
FROM (SELECT column1, 'table1' as which from table1 WHERE column1 not like 'abr%' and length(column1) < 8 
     UNION ALL 
     SELECT column1, 'table2' from table2 WHERE column1 not like 'abr%' and length(column1) < 8 
     UNION ALL 
     SELECT column1, 'table3' from table3 WHERE column1 not like 'abr%' and length(column1) < 8 
    ) t 
+0

感謝這一點,但第二個條件'和長度(column1)<8'似乎不起作用。 –

+0

你是什麼意思id不起作用?它似乎適用於所有其他答案。 –

3
SELECT Alis.column1 
    FROM (SELECT column1, 'table1' as which from table1 
    UNION ALL 
     SELECT column1, 'table2' from table2 
    UNION ALL 
     SELECT column1, 'table3' from table3 
) Alis 
WHERE column1 not like 'abr%' and length(column1) < 8;