文章目录
【MySQL】黑马教程MySQL数据库 MySQL基础(二)
本专栏主要是根据B站黑马程序员网课视频写该专栏,网课链接如下黑马程序员 MySQL数据库入门到精通,从mysql安装到mysql高级、mysql优化全囊括。
使用的软件是DataGrip,可以在官网下载,如果需要破解,可以私信联系,免费破解。(一键操作即可,懂得都懂😀😀😀)
写该专栏的目的,一方面作为个人笔记,纯手工敲出来,运行结果均为自己动手敲出,大家尽量敲一遍(没必要根据我的一样,参考网课),切勿手高眼低;另一方面,网上的教程层次不齐,有些地方看的难受,比如代码展示方面,纯文字,并且比较啰嗦,抓重点即可。(仅个人观点,勿喷😸😸😸)
函数
函数是指一段可以直接被另一段程序调用的程序或代码。
字符串函数
MySQL中内置了很多字符串函数,常用的几个如下:
函数 | 功能 |
---|---|
concat(S1, S2, ……Sn) | 字符串拼接,将S1, S2, ……Sn拼接成一个字符串 |
lower(str) | 将字符串str全部转为小写 |
upper(str) | 将字符串str全部转为大写 |
lpad(str, n, pad) | 左填充,用字符串pad对str的左边进行填充,达到n个字符串长度 |
rpad(str, n, pad) | 右填充,将字符串pad对str的右边进行填充,达到n个字符串长度 |
trim(str) | 去掉字符串头部和尾部的空格 |
substring(str, start, len) | 返回从字符串str从start位置起的len个长度的字符串 |
# concat select concat('Hello', ' MySql'); # lower select lower('Hello'); # upper select upper('Hello'); # lpad select lpad('01', 5, '-'); # rpad select rpad('01', 5, '-'); # trim select trim(' Hello MySql '); # substring select substring('Hello MySql', 1, 5);
eg:用于业务需求变更,企业员工的工号,统一为5位数,目前不足5位数的全部在前面补0,比如:1号员工的工号应该为00001.
update emp set workno = lpad(workno, 5, '0');
数值函数
常见的数值函数如下:
函数 | 功能 |
---|---|
ceil(x) | 向上取整 |
floor(x) | 向下取整 |
mod(x) | 返回x/y的模 |
rand() | 返回0~1内的随机数 |
round(x, y) | 求参数x的四舍五入的值,保留y位小数 |
# ceil select ceil(1.1); # floor select floor(1.9); # mod select mod(2,4); select mod(6,4); # rand select rand(); # round select round(2.345, 2); select round(2.344, 2);
eg:# 通过数据库的数据,生成一个六位数的随机验证码
select lpad(round(rand() * 1000000,0), 6, '0');
日期函数
常见的日期函数:
函数 | 功能 |
---|---|
curdata() | 返回当前日期 |
curtime() | 返回当前时间 |
now() | 返回当前日期和时间 |
year(date) | 获取指定date的年份 |
month(date) | 获取指定date的月份 |
day(date) | 获取指定date的日期 |
data_add(date, interval expr type) | 返回一个日期/时间值上加上一个时间间隔expr后的时间值 |
datediff(date1, date2) | 返回起始时间date1 和结束时间date2之间的天数 |
# curdate select curdate(); # curtime select curtime(); # now select now(); # year(date) select year(curtime()); # month(date) select month(curtime()); # day(date) select day(curtime()); # data_add(date, interval expr type) select date_add(curtime(), interval 70 year); select date_add(curtime(), interval 70 month); select date_add(curtime(), interval 70 day); # datediff(date1, date2) select datediff('2020-12-01','2020-11-01');
eg:查询所有员工的入职天数,并根据入职天数倒叙排序
select name ,datediff(curdate(), entrydate) as entryday from emp order by entryday desc;
流程函数
流程函数也是很常见的一类函数,可以在SQL语句中实现条件筛选,从而提高语句的效率。
函数 | 功能 |
---|---|
if(value, t, f) | 如果value为true,则返回t ,否则返回f |
ifnull(value1, value2) | 如果value1不为空,返回value1,否则返回value2 |
case when [val1] then [res1] ……else [default] end | 如果val1为true,返回res1,……否则返回default默认值 |
case [expr] when [val1] then [res1]…… else [default] end | 如果expr的值等于val1,返回res1,……否则返回default默认值 |
# if select if(true, 'ok', 'error'); select if(false, 'ok', 'error'); # ifnull select ifnull('ok', 'default'); select ifnull('', 'default'); select ifnull(null, 'default'); # case when then else end # 查询emp表的员工姓名和工作地址(北京/上海--->一线城市,其他 ----> 二线城市) select name, (case workadderss when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end) as '工作地址' from emp;
eg:统计班级各个学员的成绩,展示的规则如下
>=
85 展示优秀>=
60 展示及格- 否则,展示不及格
select name, (case when math >= 85 then '优秀' when math >= 60 then '及格' else '不及格' end) as '数学', (case when english >= 85 then '优秀' when english >= 60 then '及格' else '不及格' end) as'英语', (case when chinese >= 85 then '优秀' when chinese >= 60 then '及格' else '不及格' end) as '语文' from score;
总结
-
字符串函数
concat, lower, upper, lpad, rpad, trim, substring
-
数值函数
ceil, floor, mod, rand, round
-
日期函数
curtime, curdate, now, year, month, day, date_add, datediff
-
流程函数
if, ifnull, case[...] when ...then ...else...end
约束
概述
-
概念:约束是作用于表字段上的规则,用于限制存储在表中的数据。
-
目的:保证数据库中数据的正确、有效性和完整性。
-
分类:
约束 描述 关键字 非空约束 限制该字段的数据不能为null not null
唯一约束 保证该字段的所有数据都是唯一,不重复的 unique
主键约束 主键是一行的唯一表示,要求非空且唯一 primary key
默认约束 保存数据时,如果未指定该字段的值,则采用默认值 default
检查约束 保证字段值满足某一个条件 check
外键约束 用来让两张表的数据间之间建立连接,保证数据的一致性和完整性 foreign key
注意:约束是作用于表中字段上的,可以在创建表/修改表的时候添加约束。
约束演示
案例:根据需求,完成表结构的创建
字段名 | 字段含义 | 字段类型 | 约束条件 | 约束关键字 |
---|---|---|---|---|
id | ID唯一标识 | int | 主键,并且自动增长 | primary key,auto_increment |
name | 姓名 | varchar(10) | 不为空,并且唯一 | not null, unique |
age | 年龄 | int | 大于0,并且小于等于120 | check |
status | 状态 | char(1) | 如果没有指定该值,默认为1 | default |
gender | 性别 | char(1) | 无 |
create table user( id int primary key auto_increment comment '主键', name varchar(10) not null unique comment '姓名', age int check ( age > 0 and age <= 120 ) comment '年龄', status char(1) default '1' comment '状态', gender char(1) comment '性别' )comment '用户表'; insert into user(name, age, status, gender) values ('Tom', 19, '1', '男'), ('Jack', 25, '0', '男'); insert into user(name, age, status, gender) values ('Anson',19, '1', '男'); select * from user; -- insert into user(name, age, status, gender) values (null, 19, '1', '男'); # name不为空 -- insert into user(name, age, status, gender) values ('Anson',19, '1', '男'); # 已经存在 insert into user(name, age, status, gender) values ('Tom4', 80, '1', '男'); -- insert into user(name, age, status, gender) values ('Tom5', -1, '1', '男'); # check检查 insert into user(name, age, status, gender) values ('Tom4', 121, '1', '男'); #check检查 insert into user(name, age, gender) values ('Tom5', 80, '男'); # default语句
外键约束
外键用来让两张表的数据之间建立连接,从而保证数据的一致性和完整性。
添加外键
alter table 表名 add constraint 外键名称 foreign key(外键字段名) references 主表(主表列名);
eg:添加外键
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);
删除外键
alter table 表名 drop foreign key 外键名称;
eg:删除外键
alter table emp drop foreign key fk_emp_dept_id;
删除/更新行为
行为 | 说明 |
---|---|
no action | 当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除/更新。(与restrict 一致) |
restrict | 当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除/更新。(与no action 一致) |
cascade | 当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有,则也删除/更新外键在子表中的记录。 |
set null | 当在父表中删除对应记录时,首先检查该记录是否有对应外键,如果有则设置子表中该外键值为null (这就要求该外键允许取null ) |
set default | 父表有变更时,子表将外键列设置成一个默认的值(lnnodb不支持) |
alter table 表名 add constraint 外键名称 foreign key (外键字段) references 主表名(主表字段名) on update cascade on delete cascade;
eg:外键的删除和更新行为——级联更新
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update cascade on delete cascade ;
eg2:
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update set null on delete set null;
多表查询
多表关系
项目开发中,在进行数据库表结构设计时,会根据业务需求及业务模块之间的关系,分析并设计表结构,由于业务之间相互关联,所以各个表结构之间也存在着各种联系,基本上分为三种:
-
一对多
案例:部门 与 员工的关系
关系:一个部门对应多个员工,一个员工对应一个部门
实现:在多的一方建立外键,指向一的方的主键
# 多对多 create table student( id int auto_increment primary key comment '主键ID', name varchar(10) comment '姓名', no varchar(10) comment '学号' )comment '学生表'; insert into student values (null, '黛绮丝', '2000100101'), (null, '谢逊', '2000100102'), (null, '殷天正', '2000100103'), (null, '韦一笑', '2000100104'); create table course( id int auto_increment primary key comment '主键ID', name varchar(10) comment '课程名称' )comment '课程表'; insert into course values (null, 'Java'), (null, 'PHP'), (null, 'MySQL'), (null, 'Hadoop'); create table student_course( id int auto_increment primary key comment '主键', studentid int not null comment '学生ID', courseid int not null comment '课程ID', constraint fk_courseid foreign key (courseid) references course(id), constraint fk_studentid foreign key (studentid) references student(id) )comment '学生课程中间表'; insert into student_course values (null, 1, 1), (null, 1, 2), (null, 1, 3), (null, 2, 2), (null, 2, 3), (null, 3, 4);
-
多对多
-
一对一
案例:用户 与 用户详情的关系
关系:一对一关系,多用于单表拆分,将一张表的基础字段放在一张表中,其他详情字段放在另一张表中,以提升操作效率
实现:在任意一方假如外键,关联另外一方的外键,并且设置为唯一的(unique)
create table tb_user( id int auto_increment primary key comment '主键ID', name varchar(10) comment '姓名', age int comment '年龄', gender char(1) comment '1:男 , 2:女', phone char(11) comment '手机号' )comment '用户基本信息表'; create table tb_user_edu( id int auto_increment primary key comment '主键ID', degree varchar(20) comment '学历', major varchar(50) comment '专业', primaryschool varchar(50) comment '小学', middleschool varchar(50) comment '中学', university varchar(50) comment '大学', userid int unique comment '用户ID', constraint fk_userid foreign key (userid) references tb_user(id) )comment '用户教育信息表'; insert into tb_user(id, name, age, gender, phone) VALUES (null, '黄渤', 45, '1', '18800001111'), (null, '冰冰', 35, '2', '18800001111'), (null, '马云', 55, '1', '18800008888'), (null, '李彦宏', 50, '1', '18800009999'); insert into tb_user_edu(id, degree, major, primaryschool, middleschool, university, userid) VALUES (null, '本科', '舞蹈', '静安区第一小学', '静安区第一中学', '北京舞蹈学院', 1), (null, '硕士', '表演', '朝阳区第一小学', '朝阳区第一中学', '北京电影学院', 2), (null, '本科', '英语', '杭州市第一小学', '杭州市第一中学', '杭州师范大学', 3), (null, '本科', '应用数学', '阳泉第一小学', '阳泉第一中学', '清华大学', 4);
多表查询概念
概述:指从多张表中查询数据
笛卡尔积:笛卡尔乘积是指在数学中,两个集合A集合 和 B集合的所有组合情况。(在查询多表时,需要消除无效的笛卡尔积)
数据准备:
# 如果表存在,则先删除外键后再重建
alter table emp drop foreign key fk_emp_dept_id;
# 删除dept表
drop table if exists dept;
# 创建dept表
create table dept
(
id int auto_increment comment 'ID' primary key,
name varchar(50) not null comment '部门名称'
) comment '部门表';
insert into dept (id, name)
values (1, '研发部'),
(2, '市场部'),
(3, '财务部'),
(4, '销售部'),
(5, '总经办'),
(6, '人事部');
drop table if exists emp;
# 创建emp表
create table emp
(
id int auto_increment comment 'ID' primary key,
name varchar(50) not null comment '姓名',
age int comment '年龄',
job varchar(20) comment '职位',
salary int comment '薪资',
entrydate date comment '入职时间',
managerid int comment '直属领导ID',
dept_id int comment '部门ID',
-- 添加外键
constraint fk_emp_dept_id foreign key (dept_id) references dept (id)
) comment '员工表';
# 向emp中插入数据
insert into emp (id, name, age, job, salary, entrydate, managerid, dept_id)
values (1, '金庸', 66, '总裁', 20000, '2000-01-01', null, 5),
(2, '张无忌', 20, '项目经理', 12500, '2005-12-05', 1, 1),
(3, '杨逍', 33, '开发', 8400, '2000-11-03', 2, 1),
(4, '韦一笑', 48, '开发', 11000, '2002-02-05', 2, 1),
(5, '常遇春', 43, '开发', 10500, '2004-09-07', 3, 1),
(6, '小昭', 19, '程序员鼓励师', 6600, '2004-10-12', 2, 1),
(7, '灭绝', 60, '财务总监', 8500, '2002-09-12', 1, 3),
(8, '周芷若', 19, '会计', 48000, '2006-06-02', 7, 3),
(9, '丁敏君', 23, '出纳', 5250, '2009-05-13', 7, 3),
(10, '赵敏', 20, '市场部总监', 12500, '2004-10-12', 1, 2),
(11, '鹿杖客', 56, '职员', 3750, '2006-10-03', 10, 2),
(12, '鹤笔翁', 19, '职员', 3750, '2007-05-09', 10, 2),
(13, '方东白', 19, '职员', 5500, '2009-02-12', 10, 2),
(14, '张三丰', 88, '销售总监', 14000, '2004-10-12', 1, 4),
(15, '俞莲舟', 38, '销售', 4600, '2004-10-12', 14, 4),
(16, '宋远桥', 40, '销售', 4600, '2004-10-12', 14, 4),
(17, '陈友谅', 42, null, 2000, '2011-10-12', 1, null);
结果演示:
select * from emp, dept; # 笛卡尔积
select * from emp, dept where emp.dept_id = dept.id;
内连接
内连接查询语法:
-
隐式内连接
select 字段列表 from 表1,表2 where 条件……;
-
显式内连接
select 字段列表 from 表1 [inner] join 表2 on 连接条件……;
内连接查询的是两张表交集的部分
eg:查询每一个员工姓名,及关联的部门的名称
# 查询每一个员工的姓名,及关联的部门的名称(隐式内连接实现) select emp.name, dept.name from emp, dept where emp.dept_id = dept.id; # 查询每一个员工的姓名,及关联的部门的名称(显式内连接实现) select emp.name, dept.name from emp inner join dept where emp.dept_id = dept.id; # 正常写法 select emp.name, dept.name from emp join dept where emp.dept_id = dept.id; # 省略inner select e.name, d.name from emp e join dept d where e.dept_id = d.id; # 起别名
外连接
外连接查询语法:
-
左外连接
select 字段列表 from 表1 left [outer] join 表2 on 条件……;
相当于查询字段1(左表)的所有数据包含表1和表2交集部分的数据
-
右外连接
select 字段列表 from 表1 right [outer] join 表2 on 条件……;
相当于查询表2(右表)的所有数据包含表1和表2交集部分数据
eg:查询emp表的所有数据,和对应的部门信息(左外连接)
select emp.*, dept.name from emp left outer join dept on emp.dept_id = dept.id; select emp.*, dept.name from emp left join dept on emp.dept_id = dept.id;
eg:查询dept表的所有数据,和对应的员工信息(右外连接)
select dept.*, emp.* from emp right outer join dept on emp.dept_id = dept.id; select * from emp e right join dept d on e.dept_id = d.id;
自连接
自连接查询语法:
select 字段列表 from 表A 别名A join 表名A 别名B on 条件……;
自连接查询,可以是内连接,也可以是外连接查询。一定要起别名
eg:查询
员工
及其所属领导
的名字select a.name '员工', b.name '领导' from emp a join emp b on a.managerid = b.id;
eg:查询所有员工 emp 及其领导的名字 emp, 如果员工没有领导,也需要查询出来
select a.name '员工', b.name '领导' from emp a left join emp b on a.managerid = b.id;
联合查询 - union,union all(集合查询)
对于union查询,就是把多次查询的结果合并起来,形成一个新的查询结果集。
select 字段列表 from 表A ……
union [all]
select 字段列表 from 表B ……;
eg:将工资低于 5000 的员工, 和 年龄大于50岁的员工全部查询出来
select * from emp where salary < 5000 union all select * from emp where age > 50; # 结果重复
select * from emp where salary < 5000 union select * from emp where age > 50; # 结果无重复
对于联合查询的多张表的列数必须保持一致,字段类型也需要保持一致。
union all会将全部的数据直接合并在一起,union会对合并之后的数据去重
子查询
-
概念:SQL语句中嵌套select语句,称为
嵌套语句
,又称子查询
。select * from t1 where column1 = (select column1 from t2);
子查询外部的语句可以使insert / update / delete / select 的任何一个。
-
根据子查询结果不同,分为:
- 标量子查询(子查询结果为单个值)
- 列子查询(子查询结果为一列)
- 行子查询(子查询结果为一列)
- 表子查询(子查询结果为多行多列)
-
根据子查询位置,分为: where之后、from之后、select之后。
标量子查询
子查询返回的结果是单个值(数字、字符串、日期等),最简单地形式,这种子查询称为标量子查询
常用的操作符:=
<>
>
>=
<
<=
eg:查询
销售部
的所有员工信息# a. 查询”销售部“部门ID select id from dept where name = '销售部'; # b. 根据销售部门ID,查询员工信息 select * from emp where dept_id = 4; select * from emp where dept_id = (select id from dept where name = '销售部');
eg:查询在
方东白
入职之后的员工信息# 插入 方东白 的入职日期 select entrydate from emp where name = '方东白'; # 查询指定入职日期之后入职的员工信息 select * from emp where entrydate > (select entrydate from emp where name = '方东白');
列子查询
子查询返回的结果是一列(可以是多行),这种查询称为列子查询
常用的操作符: in
、 not in
、 any
、some
、all
操作符 | 描述 |
---|---|
in | 在指定集合范围内,多旋一 |
not in | 不在指定的集合范围之内 |
any | 子查询返回列表中,有任何一个满足即可 |
some | 与any 等同,使用some 的地方都可以使用any |
all | 子查询返回列表的所有值都必须满足 |
eg;查询
销售部
和市场部
的所有员工信息# 查询 销售部 和 市场部 的部门ID select id from dept where name = '销售部' or name = '市场部'; # 根据部门ID,查询员工信息 select * from emp where dept_id in (select id from dept where name = '市场部' or name = '销售部');
eg:查询比
财务部
所有人工资都高的员工信息# 查询所有 财务部 人员工资 select id from dept where name = '财务部'; select salary from emp where dept_id = (select id from dept where name = '财务部'); # b 比 财务部 所有人工资都高的员工 select * from emp where salary > all(select salary from emp where dept_id = (select id from dept where name = '财务部'));
eg :查询比
研发部
其中任意一人员工工资高的员工信息# 查询研发部门所有人工资 select salary from emp where dept_id = (select id from dept where name = '研发部'); # 比研发部其中任意一人工资都高的员工信息 select * from emp where salary > any(select salary from emp where dept_id = (select id from dept where name = '研发部'));
行子查询
子查询返回的结果是一行(可以是多列),这种子查询称为行子查询。
常用的操作符:=
、 <>
、 in
、 not in
eg:查询与
张无忌
的薪资及直属领导相同的员工信息# 行子查询 # 查询 张无忌 的薪资及直属领导相同的员工信息 select salary, managerid from emp where name = '张无忌'; # 查询与 张无忌 的薪资及直属领导相同的员工信息 select * from emp where (salary, managerid) = (select salary, managerid from emp where name = '张无忌');
表子查询
子查询返回的结果是多行多列,这种子查询称为表子查询。
常用的操作符:in
eg:查询与
鹿杖客、宋远桥
的职位和薪资相同的员工信息# 表子查询 # 查询 鹿杖客、宋远桥 的职位和薪资 select job, salary from emp where name = '鹿杖客' or name = '宋远桥'; # 查询与 鹿杖客、宋远桥 的职位和薪资相同的员工信息 select * from emp where (job, salary) in (select job, salary from emp where name = '鹿杖客' or name = '宋远桥');
eg:查询入职日期是
2006-01-01
之后的员工信息,及其部门信息# 表子查询 # 入职日期是 2006-01-01 之后的员工信息 select * from emp where entrydate > '2006-01-01'; # 查询这部分员工,对应的部门信息 select e.*, d.* from (select * from emp where entrydate > '2006-01-01') e left join dept d on e.dept_id = d.id;
多表查询案例
数据准备:
# 多表查询
create table salgrade(
grade int,
losal int,
hisal int
)comment '薪资等级表';
insert into salgrade
values (1, 0, 3000),
(2, 3001, 5000),
(3, 5001, 8000),
(4, 8001, 10000),
(5, 10001, 15000),
(6, 15001, 20000),
(7, 20001, 25000),
(8, 25001, 30000);
案例
eg:查询员工的姓名、年龄、职位、部门信息(隐式内连接)
# 连接条件:emp.dept_id = dept.id select emp.name, age, job, dept.name from emp ,dept where emp.dept_id = dept.id;
eg:查询年龄小于30岁的员工的姓名、年龄、职位、部门信息(显示内连接)
# 连接条件:emp.dept_id = dept.id select e.name, e.age, e.job, d.name from emp e inner join dept d on e.dept_id = d.id where e.age < 30 ;
eg:查询拥有员工的部门ID、部门名称
# 连接条件:emp.dept_id = dept.id select distinct d.id, d.name from emp e, dept d where e.dept_id = d.id;
>
eg:查询所有年龄大于40岁的员工,及其归属的部门名称;如果员工没有分配部门,也需要显示出来
# 连接条件:emp.dept_id = dept.id # 外连接 select e.*, d.name from emp e left join dept d on e.dept_id = d.id where age > 40;
eg:查询所有员工的工资等级
# 连接条件:emp.salary >= salgrade.losal and emp.salary <= salgrade.hisal select e.*, s.grade from emp e, salgrade s where e.salary >= s.losal and e.salary <= s.hisal;
eg:查询
研发部
所有员工的信息工资等级-- 查询 研发部 所有员工的信息及工资等级 -- emp dept salgrade -- 链接条件:emp.dept_id = dept.id, emp.salary > losal and emp.salary < hisal select e.*, d.name, s.grade from emp e, dept d, salgrade s where e.dept_id = d.id and e.salary > s.losal and e.salary < s.hisal and d.name = '研发部';
eg:查询
研发部
员工的平均工资-- 查询 研发部 员工的平均工资 -- 链接条件: emp.dept_id = dept.id; select avg(e.salary) from emp e, dept d where e.dept_id = d.id and d.name = '研发部';
eg:查询工资比
灭绝
高的员工信息-- 查询工资比 灭绝 高的员工信息 -- 查询 灭绝 的薪资 select salary from emp where name = '灭绝'; -- 查询比 灭绝 工资高的员工信息 select * from emp where salary > (select salary from emp where name = '灭绝');
eg:查询比平均薪资高的员工
-- 查询比平均薪资高的员工信息 -- 查询平均工资 select avg(salary) from emp; -- 查询比平均薪资高的员工 select *,(select avg(salary) from emp) '平均工资' from emp where salary > (select avg(salary) from emp);
eg:查询低于本部门平均工资的员工信息
-- 查询低于本部门平均工资的员工信息 -- 查询指定部门的平均工资 select avg(salary) from emp e1 where e1.dept_id = 1; select avg(salary) from emp e1 where e1.dept_id = 2; -- 查询低于本部门的平均工资的员工信息 select e2.*, d.name from emp e2, dept d where e2.salary < (select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id) and e2.dept_id = d.id;
eg:查询所有的部门信息,并统计部门的员工人数
-- 查询所有的部门信息,并统计部门的员工人数 -- 链接条件:emp.dept_id = dept.id; select *, (select count(*) from emp where emp.dept_id = dept.id) '人数' from dept;
eg:查询所有学生的选课情况,展示出学生名称,学号,课程名称
-- 查询所有学生的选课情况,展示出学生名称、学号、课程名称 -- 表 student、course、student_course -- 链接条件 : student.id = student_course.studentid , course.id = student_course.courseid select s.*, c.name from student s, course c, student_course sc where s.id = sc.studentid and c.id = sc.courseid;