2016-08-01 61 views
0

我想用心靈2個表連接,但是當我嘗試要麼下面的語句,我得到錯誤(1248):每一個派生表必須有它自己的別名INNER JOIN

查詢(1248)錯誤:每個派生的表必須有它自己的別名

SELECT DISTINCT(t2.col) 
FROM tab2 as t2 
INNER JOIN (
    SELECT DISTINCT(t1.col) 
    FROM tab1 as t1 
    WHERE t1.id>678 AND t1.id<5248 
) ON t2.col=t1.col 
WHERE t2.id>10 AND t2.id<3770 

SELECT DISTINCT(col) 
FROM tab2 as t2 
INNER JOIN (
    SELECT DISTINCT(col) 
    FROM tab1 as t1 
    WHERE t1.id>678 AND t1.id<5248 
) ON t2.col=t1.col 
WHERE t2.id>10 AND t2.id<3770 

什麼問題?

+1

必需的這一個別名'(SELECT DISTINCT(t1.col)FROM TAB1爲t1 WHERE t1.id> 678和t1.id <5248)' – 1000111

+0

注意DISTINCT不是功能。 – Strawberry

回答

1

你的東東到一個別名添加到您的連接表:

SELECT DISTINCT(t2.col) FROM tab2 as t2 INNER JOIN (SELECT DISTINCT(t1.col) FROM tab1 as t1 WHERE t1.id>678 AND t1.id<5248) as t3 ON t2.col=t3.col WHERE t2.id>10 AND t2.id<3770 
+0

謝謝!它看起來內部不需要額外的別名,因爲它已經是別名了SELECT DISTINCT(t2.col)FROM tab2 as t2 INNER JOIN(SELECT DISTINCT(col)FROM tab1 WHERE id> 678 AND id <5248)as t1 ON t2.col = t1.col WHERE t2.id> 10 AND t2.id <3770 – pedmillon

+1

是的,DISTINCT仍然不是一個函數。 – Strawberry

+1

...和'不'不是一個字:-) –

1

您需要提供對加入一個別名:

SELECT DISTINCT(t2.col) 
FROM tab2 as t2 
INNER JOIN (
    SELECT DISTINCT(t1.col) 
    FROM tab1 as t1 
    WHERE t1.id>678 AND t1.id<5248 
) as myJoinName ON t2.col=myJoinName.col 
WHERE t2.id>10 AND t2.id<3770 

重複第二查詢。您還需要更改加入的方式以使用此別名,因爲t1將超出加入條件的範圍。

0

錯誤查詢(1248):每一個派生表必須有它自己的別名

此錯誤是自解釋性和正在由您的子查詢沒有分配給他們的別名引起的。實際上,即使不使用它,每個派生表都需要一個別名。嘗試以下查詢:

SELECT DISTINCT t2.col 
FROM tab2 as t2 
INNER JOIN 
(
    SELECT DISTINCT t.col 
    FROM tab1 t 
    WHERE t.id > 678 AND t.id < 5248 
) t1 
    ON t2.col = t1.col 
WHERE t2.id > 10 AND t2.id < 3770 

SELECT DISTINCT col 
FROM tab2 as t2 
INNER JOIN 
(
    SELECT DISTINCT col 
    FROM tab1 as t 
    WHERE t.id > 678 AND t.id < 5248 
) t1 
    ON t2.col = t1.col 
WHERE t2.id > 10 AND t2.id < 3770 
相關問題