内连接
(1)形式一
select 字段列表
from A表 inner join B表
on 关联条件
【where 其他筛选条件】
说明:
如果不写关联条件,会出现一种现象:笛卡尔积
关联条件的个数 = n - 1,n是几张表关联
on只能和join一起用
(2) 形式二
select 字段列表
from A表 , B表
where 关联条件 【and 其他筛选条件】
#查询所有的员工的姓名和他所在部门的编号和部门的名称,不包括那些没有安排部门的员工
SELECT ename,did,dname FROM t_employee INNER JOIN t_department #错误
#错误:Column 'did' in field list is ambiguous,因为did没有说明是哪个表的
SELECT ename,t_employee.did,dname
FROM t_employee INNER JOIN t_department
ON t_employee.did = t_department.did
SELECT ename,t_employee.did,dname
FROM t_employee , t_department
WHERE t_employee.did = t_department.did
#查询所有的女员工的姓名和他所在部门的编号和部门的名称,不包括那些没有安排部门的员工
SELECT ename,t_employee.did,dname
FROM t_employee INNER JOIN t_department
ON t_employee.did = t_department.did
WHERE gender = '女';
SELECT ename,t_employee.did,dname
FROM t_employee , t_department
WHERE t_employee.did = t_department.did AND gender = '女'
#查询员工编号,员工的姓名,部门编号,部门名称,职位编号,职位名称
#需要t_employee,t_department, t_job
SELECT t_employee.eid, t_employee.`ename`, t_department.did,t_department.`dname`, t_job.`job_id`,t_job.`job_name`
FROM t_employee INNER JOIN t_department INNER JOIN t_job
ON t_employee.did = t_department.did AND t_employee.`job_id` = t_job.`job_id`
SELECT t_employee.eid, t_employee.`ename`, t_department.did,t_department.`dname`, t_job.`job_id`,t_job.`job_name`
FROM t_employee , t_department , t_job
WHERE t_employee.did = t_department.did AND t_employee.`job_id` = t_job.`job_id`