MySQL知识大总结(进阶)
一,数据库的约束
1,约束类型
| 1 | not null | 非空约束,标记这个字段不可以为空 |
| 2 | unique | 唯一约束,标记这个字段的值是该列唯一的值,在这一列的其他行,不可以与该字段相等 |
| 3 | default | 默认约束,在该字段没有赋值时,使用默认值填充该列 |
| 4 | primary key | 主键约束,相当于not null + unique |
| 5 | foreign key | 外键约束,与其他表的主键简历联系,在添加或修改数据是,会根据主外键关系检查数据是否合法 |
1,not null
试着使用not null 来创建数据表
create table if not exists student(-> id bigint not null,-> name varchar(20) not null-> );
desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | bigint | NO | | NULL | |
| name | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
我们看到在null的列,两个字段都不允许为空,我们添加两行不为空和一行为空的数据来试一下。
insert into student values (1,'张三'),(2,'李四');
select * from student;
+----+--------+
| id | name |
+----+--------+
| 1 | 张三 |
| 2 | 李四 |
+----+--------+
成功添加
insert into student values (null,null);
ERROR 1048 (23000): Column 'id' cannot be null
在新增数据发生报错,它不允许字段id为空。
2,unique
在试着用unique来创建一个表
create table if not exists student(-> id bigint unique,-> name varchar(20) unique-> );
desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | bigint | YES | UNI | NULL | |
| name | varchar(20) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
key 变成了唯一类型
我们添加三个数据,两个相同的,一个不同的
insert into student values (1,'张三'),(2,'李四');
Query OK, 2 rows affected (0.00 sec)
成功添加了,
insert into student values (1,'王五');
ERROR 1062 (23000): Duplicate entry '1' for key 'student.id'
insert into student values (3,'张三');
ERROR 1062 (23000): Duplicate entry '张三' for key 'student.name'
两个字段都设置为唯一类型的,所以无论哪一个字段重复了都不可以。
3,default
create table if not exists student(-> id bigint default 0,-> name varchar(20) default '无名氏' unique-> );
这些数据库约束是可以一起使用的,
desc student;
+-------+-------------+------+-----+-----------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+-----------+-------+
| id | bigint | YES | | 0 | |
| name | varchar(20) | YES | UNI | 无名氏 | |
+-------+-------------+------+-----+-----------+-------+
我们来添加两个啥都没有的数据
insert into student (id) values (1);
Query OK, 1 row affected (0.00 sec)mysql> insert into student (name) values ('张三');
Query OK, 1 row affected (0.00 sec)
select * from student;
+------+-----------+
| id | name |
+------+-----------+
| 1 | 无名氏 |
| 0 | 张三 |
+------+-----------+
空缺的元素都由默认值填补了。
4,primary key
可以使用auto_increment来在主键后面,这样就代表这个字段是自增主键,不用对其进行赋值,在其他列的插入时,就会自行生成对应的id,但是如果我们这行sql语句写错了的话id是不会接着上一行数据的id继续的,
create table if not exists student(-> id bigint primary key auto_increment,-> name varchar(20)-> );
复合主键
create table if not exists student(-> id bigint,-> name varchar(20),-> primary key(id,name)-> );
desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | bigint | NO | PRI | NULL | |
| name | varchar(20) | NO | PRI | NULL | |
+-------+-------------+------+-----+---------+-------+
5,foreign key
create table if not exists student(-> id bigint primary key auto_increment,-> name varchar(20),-> class_id bigint,-> foreign key (class_id) references class(id)-> );
desc student;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | bigint | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| class_id | bigint | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+----------------+
有了外键到约束我们就不能随便插入数据了
select * from class;
+----+---------+
| id | name |
+----+---------+
| 1 | java113 |
| 2 | java112 |
| 3 | java111 |
+----+---------+
我们班级表新增三个班级
select * from student;
+----+--------+----------+
| id | name | class_id |
+----+--------+----------+
| 1 | 张三 | 1 |
| 2 | 李四 | 2 |
| 3 | 王五 | 3 |
+----+--------+----------+
再给每个班级新增一个学生
我们再试着给学生表中添加一个4班的学生,但是班级表是不存在编号为4的班级的
insert into student (name,class_id) values ('张三','4');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`java113`.`student`, CONSTRAINT `student_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`))
因为外键约束,我们无法添加class表中没有的班级的学生编号
二,表的设计
1,三大范式
1,第一范式:要求表中的每一列不可再分,其实就是数据库提供的数据类型能描述这个字段,如果不满足第一范式,那么数据库就不是关系型数据库
2,第二范式:在满足第一范式的基础上,消除部分函数依赖,只能出现在有复合主键的表上,如果有一列字段只依赖于复合主键的其中一列,就需要移除这个列,单独建表。如果不满足第二范式就会出现删除异常,新增异常,更新异常,数据冗余
3,第三范式:在第二范式的基础上,消除传递依赖,这个是如果除主键外的某一列,对其他某一列存在依赖时,我们就要移除这个列
2,关系模型
1,一对一模型:
各自建立各自的表,在一个表中添加一个字段完成对另一个表的引用。
2,一对多模型:
各自建立各自的表,在一表中设置主键,在多表中设置外键于一表主键关联。
3,多对多模型:
各自建立各自的表,另外添加一个新的关系表。
我们举个多对多模型的例子,
一个class表
> create table if not exists class (-> id bigint primary key auto_increment,-> name varchar(20)-> );
一个student表
create table if not exists student(-> id bigint primary key auto_increment,-> name varchar(20)-> );
一个sorce表来关联这两个表
create table if not exists sorce(-> id bigint primary key auto_increment,-> student_id bigint,-> class_id bigint,-> stu_score decimal(3,1),-> foreign key (student_id) references student(id),-> foreign key (class_id) references class(id)-> );
这就形成了多对多关系
三,查询(进阶)
1,插入时查询
语法:
insert into 新表名 (列名) select 要复制的列名(这里不要使用括号)from 旧表名
这个我们可以用到表复制的时候
select * from student;
+----+---------+
| id | name |
+----+---------+
| 1 | 张三 |
| 2 | 0李四 |
| 3 | 王五 |
+----+---------+
我们现在创建一个新表,直接将旧表的数据复制到新表中
create table if not exists new_student(-> id bigint primary key auto_increment,-> name varchar(20)-> );
insert into new_student (id,name) select id,name from student;
2,聚合函数
select * from course;
+-----------+--------------------+
| course_id | name |
+-----------+--------------------+
| 1 | Java |
| 2 | 中国传统文化 |
| 3 | 计算机原理 |
| 4 | 语文 |
| 5 | 高阶数学 |
| 6 | 英文 |
+-----------+--------------------+
select * from class;
+----------+-------------------------+
| class_id | name |
+----------+-------------------------+
| 1 | 计算机系2019级1班 |
| 2 | 中文系2019级3班 |
| 3 | 自动化2019级5班 |
+----------+-------------------------+
select * from student;
+------------+-------+-----------------+------------------+----------+
| student_id | sn | name | mail | class_id |
+------------+-------+-----------------+------------------+----------+
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 |
| 2 | 00835 | 菩提老祖 | NULL | 1 |
| 3 | 00391 | 白素贞 | NULL | 1 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 1 |
| 5 | 00054 | 不想毕业 | NULL | 1 |
| 6 | 51234 | 好好说话 | say@qq.com | 2 |
| 7 | 83223 | tellme | NULL | 2 |
| 8 | 09527 | 老外学中文 | foreigner@qq.com | 2 |
+------------+-------+-----------------+------------------+----------+
select * from exam;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孙悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 猪悟能 | 88.0 | 98.0 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 刘玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孙权 | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
+------+-----------+---------+------+---------+
mysql> select * from emp;
+----+-----------+--------------+------------+
| id | name | role | salary |
+----+-----------+--------------+------------+
| 1 | 马云 | 老板 | 1500000.00 |
| 2 | 马化腾 | 老板 | 1800000.00 |
| 3 | 鑫哥 | 讲师 | 10000.00 |
| 4 | 博哥 | 讲师 | 12000.00 |
| 5 | 平姐 | 学管 | 9000.00 |
| 6 | 莹姐 | 学管 | 8000.00 |
| 7 | 猪悟能 | 游戏角色 | 700.50 |
| 8 | 沙和尚 | 游戏角色 | 333.30 |
+----+-----------+--------------+------------+
select * from score;
+----------+------------+-----------+-------+
| score_id | student_id | course_id | score |
+----------+------------+-----------+-------+
| 1 | 1 | 1 | 70.50 |
| 2 | 1 | 3 | 98.50 |
| 3 | 1 | 5 | 33.00 |
| 4 | 1 | 6 | 98.00 |
| 5 | 2 | 1 | 60.00 |
| 6 | 2 | 5 | 59.50 |
| 7 | 3 | 1 | 33.00 |
| 8 | 3 | 3 | 68.00 |
| 9 | 3 | 5 | 99.00 |
| 10 | 4 | 1 | 67.00 |
| 11 | 4 | 3 | 23.00 |
| 12 | 4 | 5 | 56.00 |
| 13 | 4 | 6 | 72.00 |
| 14 | 5 | 1 | 81.00 |
| 15 | 5 | 5 | 37.00 |
| 16 | 6 | 2 | 56.00 |
| 17 | 6 | 4 | 43.00 |
| 18 | 6 | 6 | 79.00 |
| 19 | 7 | 2 | 80.00 |
| 20 | 7 | 6 | 92.00 |
+----------+------------+-----------+-------+
我们拿这几个表来举例子。
1,count(列):统计一列的个数
————1,统计班级有多少同学
select count(*) from exam;
+----------+
| count(*) |
+----------+
| 7 |
+----------+
————2,统计班级收集的 qq_mail 有多少个,qq_mail 为 NULL 的数据不会计入结果
select count(mail) from student-> ;
+-------------+
| count(mail) |
+-------------+
| 4 |
+-------------+
2,Sum(列):求和
————1,统计数学成绩总分
select sum(math) from exam;
+-----------+
| sum(math) |
+-----------+
| 581.0 |
+-----------+
————2,不及格 < 60 的总分,没有结果,返回 NULL
select sum(math) from exam where math<60;
+-----------+
| sum(math) |
+-----------+
| NULL |
+-----------+
3,AVG(列):求平均值
————1,统计平均总分
select avg(english) from exam;
+--------------+
| avg(english) |
+--------------+
| 63.35714 |
+--------------+
4,MAX(列):求最大值
————1,返回英语最高分
select max(english) from exam;
+--------------+
| max(english) |
+--------------+
| 90.0 |
+--------------+
5,MIN(列):求最小值
————1,返回 > 70 分以上的数学最低分
select min(math) from exam where math>70;
+-----------+
| min(math) |
+-----------+
| 73.0 |
+-----------+
3,group by
group by 语句就是分组查询
语法:
select 字段1 字段2 from 表名 group by 字段1 ,字段2;
group by是可以使用别名的,但是where不行
select * from emp;
+----+-----------+--------------+------------+
| id | name | role | salary |
+----+-----------+--------------+------------+
| 1 | 马云 | 老板 | 1500000.00 |
| 2 | 马化腾 | 老板 | 1800000.00 |
| 3 | 鑫哥 | 讲师 | 10000.00 |
| 4 | 博哥 | 讲师 | 12000.00 |
| 5 | 平姐 | 学管 | 9000.00 |
| 6 | 莹姐 | 学管 | 8000.00 |
| 7 | 猪悟能 | 游戏角色 | 700.50 |
| 8 | 沙和尚 | 游戏角色 | 333.30 |
+----+-----------+--------------+------------+
我们使用这个表
————1,查询每个角色的最高工资、最低工资和平均工资
select role,max(salary),min(salary),avg(salary) from emp group by role;
+--------------+-------------+-------------+----------------+
| role | max(salary) | min(salary) | avg(salary) |
+--------------+-------------+-------------+----------------+
| 老板 | 1800000.00 | 1500000.00 | 1650000.000000 |
| 讲师 | 12000.00 | 10000.00 | 11000.000000 |
| 学管 | 9000.00 | 8000.00 | 8500.000000 |
| 游戏角色 | 700.50 | 333.30 | 516.900000 |
+--------------+-------------+-------------+----------------+
4,Having
我们使用group by 的时候不能使用where字句,所以出现了Having,我们使用Having来过滤条件;
————1,显示平均工资低于1500的角色和它的平均工资
select role,avg(salary) from emp group by role having avg(salary)<1500;
+--------------+-------------+
| role | avg(salary) |
+--------------+-------------+
| 游戏角色 | 516.900000 |
+--------------+-------------+
5,联合查询
对多张表进行取笛卡尔积,在选取正确的联合表;
1,内连接
语法:
select 表1 别名,表2 别名 where 连接条件 and 其他条件
select 表1 别名 [inner] join 表2 别名2 on 连接条件 and 其他条件
————1,查询“许仙”同学的 成绩
select stu.name,sc.score from student stu,score sc where sc.student_id = stu.student_id and stu.name = '许仙';
+--------+-------+
| name | score |
+--------+-------+
| 许仙 | 67.00 |
| 许仙 | 23.00 |
| 许仙 | 56.00 |
| 许仙 | 72.00 |
+--------+-------+
select stu.name,sc.score from student stu inner join score sc on stu.student_id = sc.student_id where stu.name = '许仙';
+--------+-------+
| name | score |
+--------+-------+
| 许仙 | 67.00 |
| 许仙 | 23.00 |
| 许仙 | 56.00 |
| 许仙 | 72.00 |
+--------+-------+
————2,查询所有同学的总成绩,及同学的个人信息:
select stu.name,stu.sn,stu.mail,sum(sc.score) from student stu,score sc where stu.student_id = sc.student_id group by stu.student_id;
+-----------------+-------+-----------------+---------------+
| name | sn | mail | sum(sc.score) |
+-----------------+-------+-----------------+---------------+
| 黑旋风李逵 | 09982 | xuanfeng@qq.com | 300.00 |
| 菩提老祖 | 00835 | NULL | 119.50 |
| 白素贞 | 00391 | NULL | 200.00 |
| 许仙 | 00031 | xuxian@qq.com | 218.00 |
| 不想毕业 | 00054 | NULL | 118.00 |
| 好好说话 | 51234 | say@qq.com | 178.00 |
| tellme | 83223 | NULL | 172.00 |
+-----------------+-------+-----------------+---------------+
select stu.sn,stu.name,stu.mail,sum(sc.score) from student stu inner join score sc on stu.student_id = sc.student_id group by stu.student_id;
+-------+-----------------+-----------------+---------------+
| sn | name | mail | sum(sc.score) |
+-------+-----------------+-----------------+---------------+
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 300.00 |
| 00835 | 菩提老祖 | NULL | 119.50 |
| 00391 | 白素贞 | NULL | 200.00 |
| 00031 | 许仙 | xuxian@qq.com | 218.00 |
| 00054 | 不想毕业 | NULL | 118.00 |
| 51234 | 好好说话 | say@qq.com | 178.00 |
| 83223 | tellme | NULL | 172.00 |
+-------+-----------------+-----------------+---------------+
————3,查询所有同学的成绩,及同学的个人信息
select stu.sn,stu.name,stu.mail,sc.score from student stu,score sc where stu.student_id = sc.student_id;
+-------+-----------------+-----------------+-------+
| sn | name | mail | score |
+-------+-----------------+-----------------+-------+
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 70.50 |
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 98.50 |
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 33.00 |
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 98.00 |
| 00835 | 菩提老祖 | NULL | 60.00 |
| 00835 | 菩提老祖 | NULL | 59.50 |
| 00391 | 白素贞 | NULL | 33.00 |
| 00391 | 白素贞 | NULL | 68.00 |
| 00391 | 白素贞 | NULL | 99.00 |
| 00031 | 许仙 | xuxian@qq.com | 67.00 |
| 00031 | 许仙 | xuxian@qq.com | 23.00 |
| 00031 | 许仙 | xuxian@qq.com | 56.00 |
| 00031 | 许仙 | xuxian@qq.com | 72.00 |
| 00054 | 不想毕业 | NULL | 81.00 |
| 00054 | 不想毕业 | NULL | 37.00 |
| 51234 | 好好说话 | say@qq.com | 56.00 |
| 51234 | 好好说话 | say@qq.com | 43.00 |
| 51234 | 好好说话 | say@qq.com | 79.00 |
| 83223 | tellme | NULL | 80.00 |
| 83223 | tellme | NULL | 92.00 |
+-------+-----------------+-----------------+-------+
select stu.sn,stu.name,stu.mail,sc.score from student stu inner join score sc on stu.student_id = sc.student_id;
+-------+-----------------+-----------------+-------+
| sn | name | mail | score |
+-------+-----------------+-----------------+-------+
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 70.50 |
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 98.50 |
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 33.00 |
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 98.00 |
| 00835 | 菩提老祖 | NULL | 60.00 |
| 00835 | 菩提老祖 | NULL | 59.50 |
| 00391 | 白素贞 | NULL | 33.00 |
| 00391 | 白素贞 | NULL | 68.00 |
| 00391 | 白素贞 | NULL | 99.00 |
| 00031 | 许仙 | xuxian@qq.com | 67.00 |
| 00031 | 许仙 | xuxian@qq.com | 23.00 |
| 00031 | 许仙 | xuxian@qq.com | 56.00 |
| 00031 | 许仙 | xuxian@qq.com | 72.00 |
| 00054 | 不想毕业 | NULL | 81.00 |
| 00054 | 不想毕业 | NULL | 37.00 |
| 51234 | 好好说话 | say@qq.com | 56.00 |
| 51234 | 好好说话 | say@qq.com | 43.00 |
| 51234 | 好好说话 | say@qq.com | 79.00 |
| 83223 | tellme | NULL | 80.00 |
| 83223 | tellme | NULL | 92.00 |
+-------+-----------------+-----------------+-------+
2,外连接
外连接分为左外连接和右外连接。如果联合查询,左侧的表完全显示我们就说是左外连接;右侧的表完 全显示我们就说是右外连接。
————左外连接,表1完全显示
语法:
select 字段名 from 表名1 left join 表名2 on 连接条件
————右外连接,表2完全显示
语法
select 字段名 from 表名1 right join 表名2 on 连接条件
————1,显示,“老外学中文”同学的考试成绩 ,没有考试成绩也要显示
select stu.sn,stu.name,stu.mail,sc.score from student stu left join score sc on stu.student_id = sc.student_id;
+-------+-----------------+------------------+-------+
| sn | name | mail | score |
+-------+-----------------+------------------+-------+
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 70.50 |
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 98.50 |
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 33.00 |
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 98.00 |
| 00835 | 菩提老祖 | NULL | 60.00 |
| 00835 | 菩提老祖 | NULL | 59.50 |
| 00391 | 白素贞 | NULL | 33.00 |
| 00391 | 白素贞 | NULL | 68.00 |
| 00391 | 白素贞 | NULL | 99.00 |
| 00031 | 许仙 | xuxian@qq.com | 67.00 |
| 00031 | 许仙 | xuxian@qq.com | 23.00 |
| 00031 | 许仙 | xuxian@qq.com | 56.00 |
| 00031 | 许仙 | xuxian@qq.com | 72.00 |
| 00054 | 不想毕业 | NULL | 81.00 |
| 00054 | 不想毕业 | NULL | 37.00 |
| 51234 | 好好说话 | say@qq.com | 56.00 |
| 51234 | 好好说话 | say@qq.com | 43.00 |
| 51234 | 好好说话 | say@qq.com | 79.00 |
| 83223 | tellme | NULL | 80.00 |
| 83223 | tellme | NULL | 92.00 |
| 09527 | 老外学中文 | foreigner@qq.com | NULL |
+-------+-----------------+------------------+-------+
————2,学生表、成绩表、课程表3张表关联查询
select stu.sn,stu.name,stu.mail,sc.score,co.name from student stu left join score sc on stu.student_id = sc.student_id left join course co on sc.course_id = co.course_id;
+-------+-----------------+------------------+-------+--------------------+
| sn | name | mail | score | name |
+-------+-----------------+------------------+-------+--------------------+
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 70.50 | Java |
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 98.50 | 计算机原理 |
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 33.00 | 高阶数学 |
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 98.00 | 英文 |
| 00835 | 菩提老祖 | NULL | 60.00 | Java |
| 00835 | 菩提老祖 | NULL | 59.50 | 高阶数学 |
| 00391 | 白素贞 | NULL | 33.00 | Java |
| 00391 | 白素贞 | NULL | 68.00 | 计算机原理 |
| 00391 | 白素贞 | NULL | 99.00 | 高阶数学 |
| 00031 | 许仙 | xuxian@qq.com | 67.00 | Java |
| 00031 | 许仙 | xuxian@qq.com | 23.00 | 计算机原理 |
| 00031 | 许仙 | xuxian@qq.com | 56.00 | 高阶数学 |
| 00031 | 许仙 | xuxian@qq.com | 72.00 | 英文 |
| 00054 | 不想毕业 | NULL | 81.00 | Java |
| 00054 | 不想毕业 | NULL | 37.00 | 高阶数学 |
| 51234 | 好好说话 | say@qq.com | 56.00 | 中国传统文化 |
| 51234 | 好好说话 | say@qq.com | 43.00 | 语文 |
| 51234 | 好好说话 | say@qq.com | 79.00 | 英文 |
| 83223 | tellme | NULL | 80.00 | 中国传统文化 |
| 83223 | tellme | NULL | 92.00 | 英文 |
| 09527 | 老外学中文 | foreigner@qq.com | NULL | NULL |
+-------+-----------------+------------------+-------+--------------------+
3,自连接
自连接是在同一张表中进行查询,表要起两个不同的别名
————1,显示所有“计算机原理”成绩比“Java”成绩高的成绩信息
这个我们来分析一下,计算机原理和java是课程表中,成绩是score表的内容,要取这两个表的笛卡尔积,java的course_id = 1,计算机原理的course_id = 3,这次我们不是比列而是行与行,
select * from score sc1,score sc2 where sc1.course_id = 1 and sc2.course_id = 3 and sc2.score > sc1.score and sc1.student_id = sc2.student_id;
+----------+------------+-----------+-------+----------+------------+-----------+-------+
| score_id | student_id | course_id | score | score_id | student_id | course_id | score |
+----------+------------+-----------+-------+----------+------------+-----------+-------+
| 1 | 1 | 1 | 70.50 | 2 | 1 | 3 | 98.50 |
| 7 | 3 | 1 | 33.00 | 8 | 3 | 3 | 68.00 |
+----------+------------+-----------+-------+----------+------------+-----------+-------+
4,子查询
子查询就是嵌套查询,
————1,查询与“不想毕业” 同学的同班同学:
select * from student where class_id = (select class_id from student where name = '不想毕业');
+------------+-------+-----------------+-----------------+----------+
| student_id | sn | name | mail | class_id |
+------------+-------+-----------------+-----------------+----------+
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 |
| 2 | 00835 | 菩提老祖 | NULL | 1 |
| 3 | 00391 | 白素贞 | NULL | 1 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 1 |
| 5 | 00054 | 不想毕业 | NULL | 1 |
+------------+-------+-----------------+-----------------+----------+
————2,查询“语文”或“英文”课程的成绩信息
select * from score where course_id in (select course_id from course where name = '语文' or name = '英文');
+----------+------------+-----------+-------+
| score_id | student_id | course_id | score |
+----------+------------+-----------+-------+
| 17 | 6 | 4 | 43.00 |
| 4 | 1 | 6 | 98.00 |
| 13 | 4 | 6 | 72.00 |
| 18 | 6 | 6 | 79.00 |
| 20 | 7 | 6 | 92.00 |
+----------+------------+-----------+-------+
exists 关键字
这个是如果子查询的语句有返回语句,整体查询语句就执行,反之不执行,
select * from score where not exists (select course_id from course where name = '语文' or name = '英文');
Empty set (0.00 sec)
select * from score where exists (select course_id from course where name = '语文' or name = '英文');
+----------+------------+-----------+-------+
| score_id | student_id | course_id | score |
+----------+------------+-----------+-------+
| 1 | 1 | 1 | 70.50 |
| 2 | 1 | 3 | 98.50 |
| 3 | 1 | 5 | 33.00 |
| 4 | 1 | 6 | 98.00 |
| 5 | 2 | 1 | 60.00 |
| 6 | 2 | 5 | 59.50 |
| 7 | 3 | 1 | 33.00 |
| 8 | 3 | 3 | 68.00 |
| 9 | 3 | 5 | 99.00 |
| 10 | 4 | 1 | 67.00 |
| 11 | 4 | 3 | 23.00 |
| 12 | 4 | 5 | 56.00 |
| 13 | 4 | 6 | 72.00 |
| 14 | 5 | 1 | 81.00 |
| 15 | 5 | 5 | 37.00 |
| 16 | 6 | 2 | 56.00 |
| 17 | 6 | 4 | 43.00 |
| 18 | 6 | 6 | 79.00 |
| 19 | 7 | 2 | 80.00 |
| 20 | 7 | 6 | 92.00 |
+----------+------------+-----------+-------+
————1,查询所有比“中文系2019级3班”平均分高的成绩信息:
select * from score sc where sc.score > (select avg(score) from score sc,class cl,student stu where cl.class_id = stu.class_id and sc.student_id = stu.student_id and cl.name = '中文系2019级3班');
+----------+------------+-----------+-------+
| score_id | student_id | course_id | score |
+----------+------------+-----------+-------+
| 1 | 1 | 1 | 70.50 |
| 2 | 1 | 3 | 98.50 |
| 4 | 1 | 6 | 98.00 |
| 9 | 3 | 5 | 99.00 |
| 13 | 4 | 6 | 72.00 |
| 14 | 5 | 1 | 81.00 |
| 18 | 6 | 6 | 79.00 |
| 19 | 7 | 2 | 80.00 |
| 20 | 7 | 6 | 92.00 |
+----------+------------+-----------+-------+
5,合并查询
去重union
不去重 union all
比如查询id<3,或者名字为英文的课程
select * from course where course_id<3 union select * from course where name = '英文';
+-----------+--------------------+
| course_id | name |
+-----------+--------------------+
| 1 | Java |
| 2 | 中国传统文化 |
| 6 | 英文 |
+-----------+--------------------+
union all可以去重,这里就不演示了。
相关文章:
MySQL知识大总结(进阶)
一,数据库的约束 1,约束类型 1not null非空约束,标记这个字段不可以为空2unique唯一约束,标记这个字段的值是该列唯一的值,在这一列的其他行,不可以与该字段相等3default 默认约束,在该字段没…...
【C语言】库函数常见的陷阱与缺陷(2):字符串转化函数
目录 一、atoi 函数 1.1. 功能与用法 1.2. 陷阱与缺陷 1.2.1. 输入验证不足 1.2.2. 溢出问题 1.3 安全替代 1.4. 代码示例 二、atof 函数 2.1. 功能与用法 2.2. 陷阱与缺陷 2.3. 安全使用建议 2.4. 代码示例 三、strtol 函数 3.1. 功能与用法 3.2. 陷阱与缺陷 …...
渗透测试基础
渗透测试基础是指对计算机系统、网络或应用程序进行模拟攻击,以发现其安全漏洞和潜在威胁的一种安全评估技术。通过模拟真实的攻击场景,渗透测试帮助组织了解其系统的安全弱点、验证防护措施的有效性,并提供改进建议。 渗透测试的核心概念 1…...
传奇996_53——后端ui窗口局部刷新
描述:一个大窗口,点击某个键,弹出小窗口。 小窗口中将msg存进变量中 大窗口中判断一个参数是否为null,如果不为null,说明界面不是第一次打开,而是被刷新了。就加上小窗口的那个变量 有时小窗口中还有其他…...
C++ constexpr vs const
笼统的讲 constexpr 主要用于编译时期,const用于运行时,但实际上两者都可以同时用于编译时期和运行时。 const const可以修饰全局变量,局部变量,函数参数,指针,引用,也可以修饰类成员函数&…...
【达梦数据库】存储过程调用实践案例-select
目录 前言创建表插入数据查询表中数据创建存储过程打开dbms_output包输出开关调用存储过程 前言 如果要在存储过程中执行一个SELECT语句并处理其结果,你不能直接使用EXECUTE IMMEDIATE,因为EXECUTE IMMEDIATE主要用于执行那些不返回行的语句(…...
041_Compare_Matrix_Squre_Sum_in_MATLAB中矩阵平方和的比较
矩阵平方和的计算 矩阵平方和的定义 矩阵平方和的定义是对矩阵中的每一个元素进行平方,然后求和。 对于一个矩阵 A A A,其平方和定义为: sum ∑ i 1 m ∑ j 1 n A ( i , j ) 2 \text{sum} \sum_{i1}^{m}\sum_{j1}^{n} A(i,j)^2 sumi1∑…...
TimeXplusplus——提高时间序列数据的可解释性,避免琐解和分布偏移问题的深度学习可解释性的框架
摘要 论文地址:https://arxiv.org/abs/2405.09308 源码地址:https://github.com/zichuan-liu/timexplusplus 信号传输技术的优化对于推动光通信的发展至关重要。本文将详细探讨线路编码技术的目标及其实现方式。线路编码旨在提高带宽和功率效率…...
批处理读取文本第n行并赋值给变量?--遍历所有行并赋值给变量数组
::TraceLines.bat goto :test1http://www.bathome.net/thread-27229-1-1.html#批处理如何获取txt文本中某行某列的内容/指定行指定列的内容 http://www.bathome.net/thread-47304-1-1.html#如何用批处理读取文本第二行并赋值给变量? https://github.com/npocmaka/ba…...
嵌入式入门Day26
IO Day2 IO相关函数标准文件流指针缓冲区刷新时机作业 IO相关函数 time #include <time.h>time_t time(time_t *tloc);功能:返回1970年到现在的秒数参数:计算出来的秒数;返回值;成功返回1970年到现在的秒数,失…...
【Vue3项目实战系列一】—— 从零开始一个vue3项目 vue3+javascript+vite 非常详细 手把手教学
😉 你好呀,我是爱编程的Sherry,很高兴在这里遇见你!我是一名拥有十多年开发经验的前端工程师。这一路走来,面对困难时也曾感到迷茫,凭借不懈的努力和坚持,重新找到了前进的方向。我的人生格言是…...
Python_Flask04(牛马问答平台01)
项目名称:牛马问答平台 项目简介:1.主要用来给社会上的牛马们探讨老板的恶心之处。 2. 用来学习交流。 技术手段:Python3.x Flask render_template pymysql flask_sqlalchemy........ 注意该博客为迭代项目,于最后一期展示完整…...
Java转C之并发和多线程
提纲: 概念介绍与对比概述 简述Java与C在并发和多线程方面的核心区别解释C11标准、POSIX、C11 <threads.h>、Pthread等名词 Java多线程与并发回顾 线程、Runnable、ExecutorService概念说明同步关键字与工具类含义 C并发基础 没有Java式的内置线程类…...
针对一个系统的权限管理这样的业务场景,使用各设计模式解说
通义灵码 下面将介绍如何在Java中使用不同的设计模式来实现权限管理系统。每个设计模式都有其特定的应用场景和实现方式,我们将逐一讲解。 1. 单例模式 (Singleton Pattern) 应用场景:确保权限管理服务在整个系统中只有一个实例,避免重复创…...
Android AppCompatImageView View.Gone状态切换到View.VISIBLE重新layout,Kotlin
Android AppCompatImageView View.Gone状态切换到View.VISIBLE重新layout,Kotlin import android.content.Context import android.util.AttributeSet import android.util.Log import androidx.appcompat.widget.AppCompatImageViewclass MyImageView : AppCompatI…...
在云上轻松部署达梦数据库
达梦数据库(DM Database)是由达梦数据库有限公司开发的一款关系型数据库管理系统(RDBMS)。作为国内领先的数据库产品,达梦数据库在政府、金融、能源、电信、交通、医疗、教育等多个行业得到广泛应用,尤其在…...
什么是厄尔米特(Hermitian)矩阵?
厄米矩阵(Hermitian Matrix)定义 在数学和物理中,厄米矩阵是满足以下条件的复方阵: A A † \mathbf{A}\mathbf{A}^\dagger AA† 其中, A † \mathbf{A}^\dagger A†表示矩阵 A \mathbf{A} A的共轭转置,即…...
React - useActionState、useFormStatus与表单处理
参考文档:react18.3.1官方文档 一些概念: React 的 Canary 和 Experimental 频道是 React 团队用于发布和测试新功能的渠道。 useActionState useActionState 是一个可以根据某个表单动作的结果更新 state 的 Hook。 const [state, formAction, isPe…...
v3账号密码登录随机图片验证码
安装插件 pnpm i identify --save图形验证码组件 <template><div class"s-canvas"><!-- 图形验证码的宽和高都来自于父组件的传值,若父组件没有传值,那么就按当前子组件的默认值进行渲染 --><canvas id"s-canvas&…...
不只是请求和响应:使用Fiddler解读Cookie与状态码全指南(下)
欢迎浏览高耳机的博客 希望我们彼此都有更好的收获 感谢三连支持! 不只是请求和响应:使用Fiddler抓包HTTP协议全指南(上)_fiddler 获取响应脚本-CSDN博客https://blog.csdn.net/Chunfeng6yugan/article/details/144005872?spm1001.2014.3001.5501 不只是请求和响…...
HTML 语义化
目录 HTML 语义化HTML5 新特性HTML 语义化的好处语义化标签的使用场景最佳实践 HTML 语义化 HTML5 新特性 标准答案: 语义化标签: <header>:页头<nav>:导航<main>:主要内容<article>&#x…...
Leetcode 3577. Count the Number of Computer Unlocking Permutations
Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接:3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯,要想要能够将所有的电脑解锁&#x…...
ESP32读取DHT11温湿度数据
芯片:ESP32 环境:Arduino 一、安装DHT11传感器库 红框的库,别安装错了 二、代码 注意,DATA口要连接在D15上 #include "DHT.h" // 包含DHT库#define DHTPIN 15 // 定义DHT11数据引脚连接到ESP32的GPIO15 #define D…...
spring:实例工厂方法获取bean
spring处理使用静态工厂方法获取bean实例,也可以通过实例工厂方法获取bean实例。 实例工厂方法步骤如下: 定义实例工厂类(Java代码),定义实例工厂(xml),定义调用实例工厂ÿ…...
学习STC51单片机31(芯片为STC89C52RCRC)OLED显示屏1
每日一言 生活的美好,总是藏在那些你咬牙坚持的日子里。 硬件:OLED 以后要用到OLED的时候找到这个文件 OLED的设备地址 SSD1306"SSD" 是品牌缩写,"1306" 是产品编号。 驱动 OLED 屏幕的 IIC 总线数据传输格式 示意图 …...
【Java学习笔记】BigInteger 和 BigDecimal 类
BigInteger 和 BigDecimal 类 二者共有的常见方法 方法功能add加subtract减multiply乘divide除 注意点:传参类型必须是类对象 一、BigInteger 1. 作用:适合保存比较大的整型数 2. 使用说明 创建BigInteger对象 传入字符串 3. 代码示例 import j…...
深入浅出深度学习基础:从感知机到全连接神经网络的核心原理与应用
文章目录 前言一、感知机 (Perceptron)1.1 基础介绍1.1.1 感知机是什么?1.1.2 感知机的工作原理 1.2 感知机的简单应用:基本逻辑门1.2.1 逻辑与 (Logic AND)1.2.2 逻辑或 (Logic OR)1.2.3 逻辑与非 (Logic NAND) 1.3 感知机的实现1.3.1 简单实现 (基于阈…...
Git 3天2K星标:Datawhale 的 Happy-LLM 项目介绍(附教程)
引言 在人工智能飞速发展的今天,大语言模型(Large Language Models, LLMs)已成为技术领域的焦点。从智能写作到代码生成,LLM 的应用场景不断扩展,深刻改变了我们的工作和生活方式。然而,理解这些模型的内部…...
【学习笔记】erase 删除顺序迭代器后迭代器失效的解决方案
目录 使用 erase 返回值继续迭代使用索引进行遍历 我们知道类似 vector 的顺序迭代器被删除后,迭代器会失效,因为顺序迭代器在内存中是连续存储的,元素删除后,后续元素会前移。 但一些场景中,我们又需要在执行删除操作…...
android RelativeLayout布局
<?xml version"1.0" encoding"utf-8"?> <RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"android:layout_height"match_parent"android:gravity&…...
