2016-08-01 49 views
0

würde Ich mag INNER auf 2 Tabellen verwenden JOIN, aber wenn ich eine der beiden folgenden Aussagen versuchen, erhalte ichFehler bei Abfrage (1248): Jede abgeleitete Tabelle muss einen eigenen Alias ​​INNER JOIN haben

Fehler bei der Abfrage (1248): Jede abgeleitete Tabelle muss seinen eigenen alias

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 

haben Was ist das Problem?

+1

Erforderlich einen Alias ​​dieser '(SELECT DISTINCT (t1.col) FROM tab1 als t1 WHERE t1.id> 678 UND t1.id <5248)' – 1000111

+0

Beachten Sie, dass DISTINCT ist kein Funktion. – Strawberry

Antwort

1

Sie nee einen Alias ​​zu Ihrem Join-Tabelle hinzuzufügen:

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

Danke! Und es scheint, dass die innere den zusätzlichen Alias ​​nicht benötigt, da sie bereits aliased SELECT DISTINCT (t2.col) FROM tab2 als t2 INNER JOIN (SELECT DISTINCT (col) VON tab1 WHERE id> 678 UND id <5248) als t1 ON t2.col = t1.col WHERE t2.id> 10 UND t2.id <3770 – pedmillon

+1

Ja, DISTINCT ist immer noch keine Funktion. – Strawberry

+1

... und 'ain't' ist kein Wort :-) –

1

Sie benötigen einen Alias ​​für die beitreten bieten:

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 

Wiederholen Sie für die zweite Abfrage. Sie müssen auch die Art der Verknüpfung ändern, um diesen Alias ​​zu verwenden, da t1 außerhalb des Gültigkeitsbereichs der Join-Bedingung liegt.

0

Fehler bei Abfrage (1248): Jede abgeleitete Tabelle muss seinen eigenen alias

Dieser Fehler haben, ist selbst erklärend und wird von Ihren Unterabfragen verursacht, denen kein Alias ​​zugewiesen wurde. Tatsächlich benötigt jede abgeleitete Tabelle einen Alias, auch wenn Sie sie nicht verwenden. Versuchen Sie, die folgenden Abfragen:

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