sqlite LEFT OUTER JOIN多个表

时间:2022-05-11 22:59:09

In this example we have 3 related tables on a SQLite database:

在这个例子中,我们在SQLite数据库上有3个相关的表:

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)
);

Now I need to join all tables:

现在我需要加入所有表格:

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

I have tried this solution but it doesn't run:

我试过这个解决方案,但它没有运行:

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

It gives me an error: near "ON": syntax error.

它给了我一个错误:接近“ON”:语法错误。

Any help ?

有帮助吗?

1 个解决方案

#1


28  

This is a simple misplacement of your ON statement. This conforms to SQL standard:

这是您的ON语句的简单错位。这符合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 

This is explained in further depth here.

这在此进一步深入解释。

#1


28  

This is a simple misplacement of your ON statement. This conforms to SQL standard:

这是您的ON语句的简单错位。这符合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 

This is explained in further depth here.

这在此进一步深入解释。