多表查询、多表查询_内连接
多表查询
首先创建俩个表
-- 部门表
CREATE TABLE dept(
id int PRIMARY KEY auto_increment,
name VARCHAR(20)
)
-- 员工表
INSERT INTO dept (name) VALUES('开发部'),('市场部'),('财务部');
-- 创建员工
CREATE TABLE emp(
id int PRIMARY KEY auto_increment,
NAME VARCHAR(10),
gender CHAR(1),
salary DOUBLE, -- 工资
join_date DATE,
depy_id int,
FOREIGN key (depy_id) REFERENCES dept(id)
)
INSERT INTO emp(NAME,gender,salary,join_date,depy_id) VALUES('大傻','男',7500,'2002-6-7',1);
INSERT INTO emp(NAME,gender,salary,join_date,depy_id) VALUES('小王','男',5000,'2012-1-7',2);
INSERT INTO emp(NAME,gender,salary,join_date,depy_id) VALUES('唐生','男',8500,'2000-3-7',2);
INSERT INTO emp(NAME,gender,salary,join_date,depy_id) VALUES('武士','男',5500,'1902-8-7',3);
INSERT INTO emp(NAME,gender,salary,join_date,depy_id) VALUES('小丽','女',7500,'2022-6-7',1);
SELECT * FROM emp,dept; -- 查询全部
可以看到 里面有好多无用的数据,要消除无用的数据
多表查询_内连接
多表查询的分类:
内连接查询
1,隐式内连接
SELECT * FROM emp,dept WHERE emp.depy_id = dept.id;
2,显示内连接
SELECT * FROM emp INNER JOIN dept ON emp.depy_id = dept.id