例如:按照 department_id 查询 employees(员工表)和 departments(部门表)的信息。
方式一(通用型):SELECT ... FROM ... WHERE
SELECT e.last_name,e.department_id,d.department_name
FROM employees e,departments d
WHERE e.department_id = d.department_id
方式二:SELECT ... FROM ... NATURAL JOIN ...
有局限性:会自 动连接两个 表中相同 的列(可能有 多个:department_id 和manager_id)
SELECT last_name,department_id,department_name
FROM employees
NATURAL JOIN departments
方式三:SELECT ... JOIN ... USING ...
有局限性:好于方式二,但若多表的连接列列名不同,此法不合适
SELECT last_name,department_id,department_name
FROM employees
JOIN departments
USING(department_id)
方式四:SELECT ... FROM ... JOIN ... ON ...
常用方式,较方式一,更易实现外联接(左、右、满)
SELECT last_name,e.department_id,department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
--内连接
1)
--等值连接
--不等值连接
2)
--非自连接
--自连接
--外连接
--左外连接、右外连接、满外连接
别忘了“一键三联”哈