SELECT e1.`dept_id`,e1.`Name`,e1.`salary`,d1.`NAME`FROM emp e1,
department d1
WHERE e1.dept_id=d1.id;
显式内连接
SELECT emp.id,emp.`Name`,emp.`salary`,emp.`dept_id`,department.`NAME`FROM emp INNERJOIN department ON emp.`dept_id`=department.`id`WHERE emp.`Name`='唐僧';
02外连接
左外连接
INSERTINTOdepartment(NAME)VALUES('销售部');SELECT*FROM department;-- 使用内连接查询
SELECT*FROM department d1 INNERJOIN emp e1 ON d1.`id`=e1.`dept_id`;-- 使用左外连接查询
SELECT*FROM department d1 LEFTOUTERJOIN emp e1 ON d1.`id`=e1.`dept_id`;
右外连接
(二)子查询
#创建部门表
CREATETABLEdepartment(
id INTPRIMARYKEYAUTO_INCREMENT,NAMEVARCHAR(20));INSERTINTOdepartment(NAME)VALUES('开发部'),('市场部'),('财务部');SELECT*FROM department;
#创建员工表
CREATETABLEemp(
id INTPRIMARYKEYAUTO_INCREMENT,NAMEVARCHAR(20),
gender VARCHAR(1),
salary INT,
join_date DATE,
dept_id INT,CONSTRAINT emp_dept_fk FOREIGNKEY(dept_id)REFERENCESdepartment(id));INSERTINTOemp(NAME,gender,salary,join_date,dept_id)VALUES('孙悟空','男',7200,'2013-02-24',1);INSERTINTOemp(NAME,gender,salary,join_date,dept_id)VALUES('猪八戒','男 ',3600,'2010-12-02',