首页 > 数据库 >MySQL复习2

MySQL复习2

时间:2024-09-02 19:22:15浏览次数:6  
标签:复习 -- 查询 course student MySQL id select

高级查询

准备

create database greatselect;
use greatselect;

drop table if exists `class`;
create table `class` (
    `cid` int(11) not null auto_increment,
    `caption` varchar(32) not null,
    primary key (`cid`)
)engine = innoDB AUTO_INCREMENT=5 default charset = utf8;

create table if not exists `teacher` (
    `tid` int(11) not null auto_increment,
    `tname` varchar(32) not null ,
    primary key (`tid`)
) engine = innoDB auto_increment=6 default charset = utf8;

drop table if exists `course`;
create table `course` (
    `cid` int(11) not null auto_increment,
    `cname` varchar(32) not null,
    `teacher_id` int(11) not null,
    primary key (`cid`),
    key `fk_course_teacher` (`teacher_id`),
    constraint `fk_course_teacher` foreign key (`teacher_id`) references `teacher` (`tid`)
)engine = innoDB auto_increment=5 default charset = utf8;

drop table if exists `student`;
create table `student` (
    `sid` int(11) not null auto_increment,
    `gender` char(1) not null ,
    `class_id` int(11) not null ,
    `sname` varchar(32) not null ,
    primary key (`sid`),
    key `fk_class` (`class_id`),
    constraint `fk_class` foreign key (`class_id`) references `class` (`cid`)
)engine = innoDB auto_increment=17 default charset = utf8;

drop table if exists `scocre`;
create table `score` (
    `sid` int(11) not null auto_increment,
    `student_id` int(11) not null ,
    `course_id` int(11) not null ,
    `num` int (11) not null ,
    primary key (`sid`),
    key `fk_score_student` (`student_id`),
    key `fk_course_id` (`course_id`),
    constraint `fk_score_student` foreign key (`student_id`) references `student` (`sid`),
    constraint `fk_score_course` foreign key (`course_id`) references `course` (`cid`)
)engine = innoDB auto_increment=53 default charset = utf8;

show tables;

基础查询

-- 全部查询
select * from student;
-- 只查询部分字段
select `sname`, `class_id` from student;
-- 别名 列名 不要用关键字
select `sname` as ‘姓名’, `class_id` as '班级ID' from student;
-- 把查询出来的结果的重复记录去掉
select distinct `class_id` from student;

条件查询

-- 查询姓名为lennlouis的学生信息
select * from `student` where `name` = 'lennlouis';
-- 查询性别为 男,且班级为 2 的学生信息
select * from `student` where `gender` = '男' and `class_id` = 2;

范围查询

-- 查询班级id 1 到 3 的学生信息
select * from `student` where `class_id` between 1 and 3;

判空查询

-- is null 判断造成索引失效
select * from `student` where `class_id` is not null;
select * from `student` where `class_id` is null;

-- 字符串不为空
select * from `student` where `gender` <> '';
-- 字符串为空
select * from `student` where `gender` = '';

模糊查询

-- 使用 like关键字,“%”代表任意数量的字符,“_”代表占位符
-- 查询名字为 m开头的学生信息
select * from `teacher` where `tname` like 'l%';
-- 查询姓名里第二个字为 ‘e’的小学生的信息
select * from `teacher` where `tname` like '_e%';

分页查询

-- 分页查询主要用于查看第 N条 到第 M条的信息,通常和排序查询一起使用
-- 使用limit关键字,第一个参数表示从条记录开始显示,第二个参数表示要显示的数目。表中默认第一条记录的参数为0
-- 查询第二条到第三条内容
select * from `student` limit 1, 2;

查询后排序

-- 关键字:order by field, asc:升序, desc:降序
select * from `score` order by `num` asc;
\-- 多个字段排序
select * from `score` order by `course_id` desc, `num` desc

聚合查询

聚合函数描述
sum ()计算某一列的总和
avg ()计算某一列的平均值
max ()某一列的最大值
min ()某一列的最小值
count ()某一列的行数
select sum(`num`) from `score`;
select avg(`num`) from `score`;
select max(`num`) from `score`;
select min(`num`) from `score`;
select count(`num`) from `score`;

分组查询

-- 分组加 group_concat
select `gender`, group_concat(`age`) as ages from `student` group by `gender`;
-- 可以把查询出来的结果根据某个条件来分组显示
select `gender` from `student` group by `gender`;
-- 分组加聚合
select `gender`, count(*) as num from `student` group by `gender`;
-- 分组条件
select `gender`, count(*) as num from `student` group by `gender` having num > 6;

联表查询

image.png

INNER JOIN

取两张表有对应关系的记录

select
	cid
from
	`course`
inner join `teacher` on course.teacher_id = teacher.tid;

LEFT JOIN

在内联的基础上保留左边表上没有对应关系的记录

select 
	course.cid
from
	`course`
left join `teacher` on course.teacher_id = teacher.tid;

RIGHT JOIN

在内联的基础上保留右边表上没有对应关系的记录

select
	course.cid
from
	`course`
right join `teacher` on course.teacher_id = teacher.tid;

子查询/合并查询

单行子查询

select * from course where course.teacher_id = (select tid from teacher where tname = 'wood');

多行子查询

多行子查询返回多行记录的子查询

  • IN 关键字:运算符可以检测结果集中是否存在特定的值,如果检测成功就执行外部的查询。
  • EXISTS 关键字:内层查询语句不返回查询记录。而是返回一个真假值。如果内层查询语句查询到满足条件的记录,就返回一个真值(true),否则,将返回一个假值(false)。当返回的值为 true 时,外层查询语句将进行查询;当返回的值为 false 时,外层查询语句不进行查询或查询不出任何记录。
  • ALL 关键字:表示满足所有条件。使用 ALL 关键字时,只有满足内层查询语句返回的所有结果,才可以执行外层查询语句。
  • ANY 关键字:允许创建一个表达式,对子查询的返回值列表,进行比较,只要满足内层子查询条件中的任意一个比较条件,就返回一个结果作为外层查询条件。
  • 在 FROM 子句中使用子查询:子查询出现在 from 子句中,这种情况下将子查询当做一个临时表使用。
select * from student where class_id in (select cid from course where teacher_id = 2)

select * from student where exists(select cid from course where cid = 5);

select student_id, sname from (select * from score where course_id = 1 or course_id = 2) as A left join student on A.student_id = student.sid;

标签:复习,--,查询,course,student,MySQL,id,select
From: https://blog.csdn.net/H520xcodenodev/article/details/141827956

相关文章

  • MySQL索引学习总结
    1.什么是索引?MySQL官方定义:索引是帮助MySQL高效获取数据的数据结构。即:索引是数据结构!!!2.索引有哪几种数据结构?6种。二叉树、平衡二叉树、红黑树、BTree、B+Tree、Hash旧金山大学数据结构可视化网站:DataStructureVisualization(usfca.edu)二叉树对半搜索,每个节点最多......
  • 20240902_171049 mysql 填空题 ddl表
    创建一个名为tb的表creatatabletb()创建一个名为tb的表,先判断再创建createtableifnotexiststb()新建一个student表,拷备teacher表的结构createtablestudentliketeacher删除一个名为student的表droptablestudent删除名为student的表,先判断再删除droptableif......
  • 基于SpringBoot+MySQL+SSM+Vue.js的学生选课系统
    获取见最下方名片获取见最下方名片获取见最下方名片演示视频基于SpringBoot+MySQL+SSM+Vue.js的学生选课系统(附论文)技术描述开发工具:Idea/Eclipse数据库:MySQLJar包仓库:Maven前端框架:Vue/ElementUI后端框架:Spring+SpringMVC+Mybatis+SpringBoot文字描......
  • 20240902_162002 mysql 填空题 数据类型
    定义一个int类型的列,名为ageageint定义一个无符号的int类型的列,名为scorescoreintunsigned定义一个单精度小数类型的列,名为weightweightfloat定义一个双精度小数类型的列,名为weightweightdouble定义一个小数列,要求总长度为5,小数部分为2,列名为weightweightdecimal(5,......
  • MySQL服务端innodb_buffer_pool_size配置参数
    innodb_buffer_pool_size是什么?innodb_buffer_pool是InnoDB缓冲池,是一个内存区域保存缓存的InnoDB数据为表、索引和其他辅助缓冲区。innodb_buffer_pool_size是这个缓冲池的大小,默认128M(即134217728bytes)。innodb_buffer_pool_size有什么用?如果不设置innodb_buffer_pool_......
  • 基于Django的MySQL项目建设计划
    构建一个基于Django和MySQL的项目需要经过多个阶段的规划和实施。以下是一个详细的建设计划,分为项目准备、开发、测试和部署等几个关键阶段。1、问题背景为了完成大学的“问答网站”项目,需要在几天内完成项目的计划,并于下周二准备好代码的第一个版本。项目的最终截止日期约为......
  • 如何在 MySQL 中匹配列
    在MySQL中,匹配列可以通过多种方式实现,具体取决于你要执行的操作类型。常见的列匹配操作包括条件查询、JOIN操作、字符串匹配等。以下是具体解决的几种方式。1、问题背景在MySQL中,可以使用"="运算符来匹配列。例如:SELECT*FROMmytableWHEREcolumn1=column2;但是,如果col......
  • 20240905_000339 mysql 存储过程 用户自定义变量
    自定义变量的特点一个@符号定义自定变量打印自定变量另一种定义方式查询赋值......
  • 20240905_010339 mysql 存储过程 局部变量
    ......
  • MySQL索引
    索引入门深入理解MySQL索引:底层数据结构与算法详解索引基本介绍索引(index)是MySQL中高效获取数据的树结构(有序),数据库索引允许快速访问数据库表中的特定信息。没有索引,数据库系统必须对表中的每一行数据进行扫描,以找到匹配的行。这种全表扫描在数据量较小时尚可接受,但随着数据量的增......