尚硅谷之MySQL基础
内连接(INNER JOIN)
有两种,显式的和隐式的,返回连接表中符合连接条件和查询条件的数据行
格式:
隐式:SELECT [cols_list] from 表1,表2 where [condition]
显式:SELECT [cols_list] from 表1 INNER JOIN 表2 ON [关联条件] where [其他筛选条件]
SELECT [cols_list] from 表1 CROSS JOIN 表2 ON [关联条件] where [其他筛选条件]
SELECT [cols_list] from 表1 JOIN 表2 ON [关联条件] where [其他筛选条件]
#内连接 #查询员工姓名和所在部门名称 SELECT ename,dname FROM t_employee,t_department WHERE t_employee.dept_id=t_department.did; SELECT ename,dname FROM t_employee INNER JOIN t_department ON t_employee.dept_id=t_department.did; SELECT ename,dname FROM t_employee CROSS JOIN t_department ON t_employee.dept_id=t_department.did; SELECT ename,dname FROM t_employee JOIN t_department ON t_employee.dept_id=t_department.did; #查询员工姓名,基本工资,部门名称 SELECT ename,basic_salary,dname FROM t_employee,t_department,t_salary WHERE t_employee.dept_id=t_department.did AND t_employee.eid=t_salary.eid; SELECT ename,basic_salary,dname FROM t_employee INNER JOIN t_department INNER JOIN t_salary ON t_employee.dept_id=t_department.did AND t_employee.eid=t_salary.eid; |
|
外连接(OUTER JOIN)
外连接分为:
左外连接(LEFT OUTER JOIN),简称左连接(LEFT JOIN)
右外连接(RIGHT OUTER JOIN),简称右连接(RIGHT JOIN)
全外连接(FULL OUTER JOIN),简称全连接(FULL JOIN)。