SQL语句的多表查询方式(总结)

例如:按照 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 					


你可能感兴趣的:(SQL语句的多表查询方式(总结))