2012-06-19 15 views
10

In questo esempio abbiamo 3 tabelle correlate in un database SQLite:sqlite LEFT OUTER JOIN più tabelle

CREATE TABLE test1 (
    c1 integer, 
    primary key (c1) 
); 
CREATE TABLE test2 (
    c1 integer, 
    c2 integer, 
    primary key (c1, c2) 
);  
CREATE TABLE test3 (
    c2 integer, 
    c3 integer, 
    primary key (c2) 
); 

Ora ho bisogno di unire tutte le tabelle:

 test1 -> test2 (with c1 column) 
      test2 -> test3 (with c2 column).

ho provato questa soluzione, ma non viene eseguito:

SELECT 
    * 
    FROM test1 a 
     LEFT OUTER JOIN test2 b 
         LEFT OUTER JOIN test3 c 
          ON c.c2 = b.c2 
      ON b.c1=a.c1 

mi dà un errore: near "ON": syntax error.

Qualsiasi aiuto?

+1

sqlite3 mai usato, ma la sintassi SQL standard corretto è LEFT OUTER JOIN ... ON ... LEFT OUTER JOIN ... ON. .., e non annidati.) – raina77ow

+0

Ho usato questa sintassi su Oracle, PostgreSQL e SQLServer senza alcun problema. – ferpega

+1

Join nidificati - o 'JOIN (one JOIN two JOIN three) ON (one.id = two.id AND two.some_id = three.some_id)'? – raina77ow

risposta

28

Si tratta di un semplice smarrimento della dichiarazione ON. Ciò è conforme allo standard SQL:

SELECT * 
FROM test1 a 
LEFT OUTER JOIN test2 b ON b.c1=a.c1 
LEFT OUTER JOIN test3 c ON c.c2=b.c2 

Questo è spiegato in further depth here.

+0

Grazie Nathaniel funziona bene. Ho usato il formato sql precedente per anni. Ma a SQLite non piace molto. – ferpega

+0

È possibile che specifiche (fantasiose?) Implementazioni siano in grado di analizzarlo, ma che SQLite si limita a soddisfare le specifiche. –