# 创建一个user表 create table users( id int primary key auto_increment, name varchar(20), dpt_id int ); # 向user表中插入数据 insert into users values (null,'刘备',1), (null,'关羽',2), (null,'赵飞',2), (null,'吕布',2), (null,'小乔',3), (null,'大乔',3); # 创建一个department表 create table department( id int primary key auto_increment, name varchar(20) ); # 向department表中插入数据 insert into department values (null,'指挥部'), (null,'作战部'), (null,'后勤部'); # 查询 users 表中的第三和第四条数据 (第一个数字是偏移量,第二个数字是查询几条) select * from users limit 2,2; # 为users表取别名 select u.name from users as u; # 为users表中的字段取别名 select name as '姓名', dpt_id as '部门' from users; # 表的关联查询 查询users表中员工的姓名和所在的部门 select u.name as '姓名',d.name as '部门' from users as u inner join department as d where u.dpt_id = d.id; # 表的关联查询 查询users表中吕布所在的部门 (on 可以换成 where) select d.name from department as d inner join users as u on u.dpt_id=d.id and u.name='吕布';