SQLite 的 Joins 子句用于结合两个或多个数据库中表的记录。JOIN 是一种通过共同值来结合两个表中字段的手段。
SQL 定义了三种主要类型的连接:
在我们继续之前,让我们假设有两个表 COMPANY 和 DEPARTMENT。我们已经看到了用来填充 COMPANY 表的 INSERT 语句。现在让我们假设 COMPANY 表的记录列表如下:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
另一个表是 DEPARTMENT,定义如下:
1 2 3 4 5 |
<span class="pln">CREATE TABLE DEPARTMENT</span><span class="pun">(</span><span class="pln"> ID INT PRIMARY KEY NOT NULL</span><span class="pun">,</span><span class="pln"> DEPT CHAR</span><span class="pun">(</span><span class="lit">50</span><span class="pun">)</span><span class="pln"> NOT NULL</span><span class="pun">,</span><span class="pln"> EMP_ID INT NOT NULL </span><span class="pun">);</span> |
下面是填充 DEPARTMENT 表的 INSERT 语句:
1 2 3 4 5 6 7 8 |
<span class="pln">INSERT INTO DEPARTMENT </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln"> DEPT</span><span class="pun">,</span><span class="pln"> EMP_ID</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span> <span class="str">'IT Billing'</span><span class="pun">,</span> <span class="lit">1</span> <span class="pun">);</span><span class="pln"> INSERT INTO DEPARTMENT </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln"> DEPT</span><span class="pun">,</span><span class="pln"> EMP_ID</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span> <span class="str">'Engineering'</span><span class="pun">,</span> <span class="lit">2</span> <span class="pun">);</span><span class="pln"> INSERT INTO DEPARTMENT </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln"> DEPT</span><span class="pun">,</span><span class="pln"> EMP_ID</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span> <span class="str">'Finance'</span><span class="pun">,</span> <span class="lit">7</span> <span class="pun">);</span> |
最后,我们在 DEPARTMENT 表中有下列的记录列表:
1 2 3 4 5 |
<span class="pln">ID DEPT EMP_ID </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">1</span> <span class="lit">2</span> <span class="typ">Engineerin</span> <span class="lit">2</span> <span class="lit">3</span> <span class="typ">Finance</span> <span class="lit">7</span> |
交叉连接(CROSS JOIN)把第一个表的每一行与第二个表的每一行进行匹配。如果两个输入表分别有 x 和 y 列,则结果表有 x+y 列。由于交叉连接(CROSS JOIN)有可能产生非常大的表,使用时必须谨慎,只在适当的时候使用它们。
下面是交叉连接(CROSS JOIN)的语法:
1 |
<span class="pln">SELECT </span><span class="pun">...</span><span class="pln"> FROM table1 CROSS JOIN table2 </span><span class="pun">...</span> |
基于上面的表,我们可以写一个交叉连接(CROSS JOIN),如下所示:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT EMP_ID</span><span class="pun">,</span><span class="pln"> NAME</span><span class="pun">,</span><span class="pln"> DEPT FROM COMPANY CROSS JOIN DEPARTMENT</span><span class="pun">;</span> |
上面的查询会产生以下结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<span class="pln">EMP_ID NAME DEPT </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Paul</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">Paul</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">Allen</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">Allen</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">Teddy</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Teddy</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">Teddy</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">Mark</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Mark</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">Mark</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">David</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">David</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">David</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">Kim</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Kim</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">Kim</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">James</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">James</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">James</span> <span class="typ">Finance</span> |
内连接(INNER JOIN)根据连接谓词结合两个表(table1 和 table2)的列值来创建一个新的结果表。查询会把 table1 中的每一行与 table2 中的每一行进行比较,找到所有满足连接谓词的行的匹配对。当满足连接谓词时,A 和 B 行的每个匹配对的列值会合并成一个结果行。
内连接(INNER JOIN)是最常见的连接类型,是默认的连接类型。INNER 关键字是可选的。
下面是内连接(INNER JOIN)的语法:
1 |
<span class="pln">SELECT </span><span class="pun">...</span><span class="pln"> FROM table1 </span><span class="pun">[</span><span class="pln">INNER</span><span class="pun">]</span><span class="pln"> JOIN table2 ON conditional_expression </span><span class="pun">...</span> |
为了避免冗余,并保持较短的措辞,可以使用 USING 表达式声明内连接(INNER JOIN)条件。这个表达式指定一个或多个列的列表:
1 |
<span class="pln">SELECT </span><span class="pun">...</span><span class="pln"> FROM table1 JOIN table2 USING </span><span class="pun">(</span><span class="pln"> column1 </span><span class="pun">,...</span> <span class="pun">)</span> <span class="pun">...</span> |
自然连接(NATURAL JOIN)类似于 JOIN…USING,只是它会自动测试存在两个表中的每一列的值之间相等值:
1 |
<span class="pln">SELECT </span><span class="pun">...</span><span class="pln"> FROM table1 NATURAL JOIN table2</span><span class="pun">...</span> |
基于上面的表,我们可以写一个内连接(INNER JOIN),如下所示:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT EMP_ID</span><span class="pun">,</span><span class="pln"> NAME</span><span class="pun">,</span><span class="pln"> DEPT FROM COMPANY INNER JOIN DEPARTMENT ON COMPANY</span><span class="pun">.</span><span class="pln">ID </span><span class="pun">=</span><span class="pln"> DEPARTMENT</span><span class="pun">.</span><span class="pln">EMP_ID</span><span class="pun">;</span> |
上面的查询会产生以下结果:
1 2 3 4 5 |
<span class="pln">EMP_ID NAME DEPT </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">James</span> <span class="typ">Finance</span> |
外连接(OUTER JOIN)是内连接(INNER JOIN)的扩展。虽然 SQL 标准定义了三种类型的外连接:LEFT、RIGHT、FULL,但 SQLite 只支持 左外连接(LEFT OUTER JOIN)。
外连接(OUTER JOIN)声明条件的方法与内连接(INNER JOIN)是相同的,使用 ON、USING 或 NATURAL 关键字来表达。最初的结果表以相同的方式进行计算。一旦主连接计算完成,外连接(OUTER JOIN)将从一个或两个表中任何未连接的行合并进来,外连接的列使用 NULL 值,将它们附加到结果表中。
下面是左外连接(LEFT OUTER JOIN)的语法:
1 |
<span class="pln">SELECT </span><span class="pun">...</span><span class="pln"> FROM table1 LEFT OUTER JOIN table2 ON conditional_expression </span><span class="pun">...</span> |
为了避免冗余,并保持较短的措辞,可以使用 USING 表达式声明外连接(OUTER JOIN)条件。这个表达式指定一个或多个列的列表:
1 |
<span class="pln">SELECT </span><span class="pun">...</span><span class="pln"> FROM table1 LEFT OUTER JOIN table2 USING </span><span class="pun">(</span><span class="pln"> column1 </span><span class="pun">,...</span> <span class="pun">)</span> <span class="pun">...</span> |
基于上面的表,我们可以写一个外连接(OUTER JOIN),如下所示:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT EMP_ID</span><span class="pun">,</span><span class="pln"> NAME</span><span class="pun">,</span><span class="pln"> DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT ON COMPANY</span><span class="pun">.</span><span class="pln">ID </span><span class="pun">=</span><span class="pln"> DEPARTMENT</span><span class="pun">.</span><span class="pln">EMP_ID</span><span class="pun">;</span> |
上面的查询会产生以下结果:
1 2 3 4 5 6 7 8 9 10 11 |
<span class="pln">EMP_ID NAME DEPT </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="typ">Engineerin</span> <span class="typ">Teddy</span> <span class="typ">Mark</span> <span class="typ">David</span> <span class="typ">Kim</span> <span class="lit">7</span> <span class="typ">James</span> <span class="typ">Finance from:<a href="http://www.runoob.com/sqlite/sqlite-joins.html">http://www.runoob.com/sqlite/sqlite-joins.html</a></span> |