select * from employee inner join location on employee.empID = location.empID
OR
select * from employee, location where employee.empID = location.empID
内部连接 -> 从父表和子表中取出仅有的通用记录父表的 WHERE 主键与子表中的 Foreign key 匹配。
左连接 ->
伪代码
1.Take All records from left Table
2.for(each record in right table,) {
if(Records from left & right table matching on primary & foreign key){
use their values as it is as result of join at the right side for 2nd table.
} else {
put value NULL values in that particular record as result of join at the right side for 2nd table.
}
}
SELECT * FROM table1, table2 WHERE table1.id = table2.id
--[OR]
SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id
--[Results:]
id | name | id | name
---+------+----+------
1 | a1 | 1 | a2
左[外]加入:
当您希望在上面的结果中包含一个表的所有行(具有相同的关系)时,可以使用 LEFT JOIN:
(对于 好的,只需改变表的位置)
SELECT * FROM table1, table2 WHERE table1.id = table2.id
UNION ALL
SELECT *, Null, Null FROM table1 WHERE Not table1.id In (SELECT id FROM table2)
--[OR]
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id
--[Results:]
id | name | id | name
---+------+------+------
1 | a1 | 1 | a2
2 | b1 | Null | Null
完全外接:
当您还希望在结果中包含其他表的所有行时,可以使用 FULL OUTER JOIN:
SELECT * FROM table1, table2 WHERE table1.id = table2.id
UNION ALL
SELECT *, Null, Null FROM table1 WHERE Not table1.id In (SELECT id FROM table2)
UNION ALL
SELECT Null, Null, * FROM table2 WHERE Not table2.id In (SELECT id FROM table1)
--[OR] (recommended for SQLite)
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id
UNION ALL
SELECT * FROM table2 LEFT JOIN table1 ON table2.id = table1.id
WHERE table1.id IS NULL
--[OR]
SELECT * FROM table1 FULL OUTER JOIN table2 On table1.id = table2.id
--[Results:]
id | name | id | name
-----+------+------+------
1 | a1 | 1 | a2
2 | b1 | Null | Null
Null | Null | 3 | b2
empid name dept_id salary
1 Rob 1 100
2 Mark 1 300
3 John 2 100
4 Mary 2 300
5 Bill 3 700
6 Jose 6 400
税务局
deptid name
1 IT
2 Accounts
3 Security
4 HR
5 R&D
内部连接:
在 sql 查询中,大部分都是以 加入的形式编写的,它只返回表之间的匹配记录。
找出所有员工及其部门名称:
Select a.empid, a.name, b.name as dept_name
FROM emp a
JOIN department b
ON a.dept_id = b.deptid
;
empid name dept_name
1 Rob IT
2 Mark IT
3 John Accounts
4 Mary Accounts
5 Bill Security
如上所述,在输出中没有从 电磁脉冲打印 Jose,因为它的 dept _ id 6在 Department 表中没有找到匹配项。类似地,HR和 R&D行不会从 税务局表中打印出来,因为它们在 Emp 表中没有找到匹配的行。
因此,INERJOIN 或者只是 JOIN 只返回匹配的行。
左连接:
这将返回 LEFT 表中的所有记录,并且只匹配右表中的记录。
Select a.empid, a.name, b.name as dept_name
FROM emp a
LEFT JOIN department b
ON a.dept_id = b.deptid
;
empid name dept_name
1 Rob IT
2 Mark IT
3 John Accounts
4 Mary Accounts
5 Bill Security
6 Jose
因此,如果您观察上面的输出,那么 LEFT 表(Emp)中的所有记录都只打印右表中的匹配记录。
HR和 R&D行没有从 税务局表中打印出来,因为它们在 dept _ id 的 Emp 表中没有找到匹配的行。
CREATE TABLE citizen (id NUMBER,
name VARCHAR2(20),
postal NUMBER, -- <-- could do with a redesign to postalcode.id instead.
leader NUMBER);
CREATE TABLE postalcode (id NUMBER,
postal NUMBER,
city VARCHAR2(20),
area VARCHAR2(20));
INSERT INTO citizen (id, name, postal, leader)
SELECT 1, 'Smith', 2200, null FROM DUAL
UNION SELECT 2, 'Green', 31006, 1 FROM DUAL
UNION SELECT 3, 'Jensen', 623, 1 FROM DUAL;
INSERT INTO postalcode (id, postal, city, area)
SELECT 1, 2200, 'BigCity', 'Geancy' FROM DUAL
UNION SELECT 2, 31006, 'SmallTown', 'Snizkim' FROM DUAL
UNION SELECT 3, 31006, 'Settlement', 'Moon' FROM DUAL -- <-- Uuh-uhh.
UNION SELECT 4, 78567390, 'LookoutTowerX89', 'Space' FROM DUAL;
使用 JOIN和 WHERE时边界模糊
交叉连接
CROSS JOIN的结果是一行行的总体思路/INNER JOIN:
SELECT *
FROM citizen c
CROSS JOIN postalcode p
WHERE c.postal = p.postal -- < -- The WHERE condition is limiting the resulting rows
使用 CROSS JOIN获得 LEFT OUTER JOIN的结果需要一些技巧,比如添加 NULL行。
内连接
INNER JOIN成为一个笛卡尔产品,它与 The General Ideas/CROSS JOIN相同:
SELECT *
FROM citizen c
JOIN postalcode p ON 1 = 1 -- < -- The ON condition makes it a CROSS JOIN
在这种情况下,内部连接实际上可以看作是交叉连接,其结果与删除的条件不匹配。这里没有删除任何结果行。
使用 INNER JOIN来获得 LEFT OUTER JOIN的结果也需要技巧。
左外连接
LEFT JOIN的结果如下:
SELECT *
FROM citizen c
LEFT JOIN postalcode p ON 1 = 1 -- < -- The ON condition makes it a CROSS JOIN
LEFT JOIN的结果如下:
SELECT *
FROM citizen c
LEFT JOIN postalcode p ON c.postal = p.postal
WHERE p.postal IS NOT NULL -- < -- removed the row where there's no mathcing result from postalcode
SELECT *
FROM citizen c
CROSS JOIN postalcode p
WHERE c.name = 'Smith'
INTERSECT
SELECT *
FROM citizen c
CROSS JOIN postalcode p
WHERE p.area = 'Moon';