mysql 学习第二天 SQL语句

1.select 查询关键字

#:代表注释

select 字段 from 表名

查询的东西用逗号隔开

查询所有的列

sql 复制代码
SELECT name,sex from student
sql 复制代码
select * from student;

2.条件查询

条件查询 where 查询名字叫张三的人的信息

where 条件列 运算符 值

字符类型要使用' '

sql 复制代码
SELECT * from student where name = '张三'

#查询年龄大于20岁的同学信息

sql 复制代码
select * from student where age > 20;#> < >= <=

#查询年龄不等于20岁的同学

sql 复制代码
select * from student where age <> 20;
select * from student where age != 20;

要查询的条件列的值介于两个值之间 between......and...... \[\]闭区间
sql不区分大小写

sql 复制代码
select * from student where age BETWEEN 20 AND 22

in 条件列的区间是否包含在这当中---->同时怕匹配多个值

sql 复制代码
select * from student where id in (1,2,3,4)
select * from student where name in ('张三','李四','王五');

判断字段是否为空 is null

查询class_num字段是否有为空的

sql 复制代码
select * from student where class_num is null

涉及道多个条件则么办

查询出20201001班的男同学 和-->and 或-->or

sql 复制代码
select * from student where class_num = '20201001' and sex='男'

查询出20201001班的男同学或其他班的女同学

sql 复制代码
select * from student where sex = '女' and class_num != '20201001' or class_num = '20201001' and sex='男'

3.模糊查询----->搜索(根据关键词进行搜索)

查询出学生中姓王的同学 选中运行!!!

%王%向前向后匹配 name%向后匹配

sql 复制代码
select * from student where name like '王%'

_只能匹配一个字符 %可以匹配多个字符

sql 复制代码
select * from student where name like '王_'

分组/聚合查询

常见聚合函数 ---->一般针对数据类型

sum()求和

avg()求取平均数

max() 取最大值

min()取最小值

count() 取记录数

查询出同学们当中年龄最大值 --->查询的只是一个值

sql 复制代码
select max(age) from student;

count操作 查询表当中有多少数据

count(*)查询表当中所有数据 count(字段名)不会统计为null的记录

sql 复制代码
select count(*) from student;
sql 复制代码
select count(class_num) from student;

4.分组---->group by 字段 根据某一个字段进行分组

查询出各个班级的平均年龄

sql 复制代码
select avg(age) avg_age,class_num from student group by class_num;

查询出各个班男女同学的数量

sql 复制代码
select count(*) sex_count,sex,class_num from student GROUP BY class_num,sex

5.排序

默认的数据排序规则根据id从小到大进行排序

sql 复制代码
select * from student;

新插入的数据id是大的,新插入的数据往往在末尾

想要的是新插入在前

使用ORDER BY

根据age字段从小到大

sql 复制代码
select * from student ORDER BY age

根据age字段从大到小排序--->倒序 desc

sql 复制代码
select * from student ORDER BY age DESC

6.分页查询 (限制查询)limit pageSize offset pageStar

pageSize 每页查询的条数

pageStart 从那一条数据源开始 默认从0开始

假设每页显示5条

sql 复制代码
select * from studnet limit 5 offset 0;#第一页
select * from studnet limit 5 offset 5;#第二页
select * from studnet limit 5 offset 10;#第三页
select * from studnet limit 5 offset 15;#第四页

另一种写法 limit pageStart,pageSize

sql 复制代码
select * from student limit 0 , 5;#第一页
select * from student limit 5 , 5;#第二页
select * from student limit 10 , 5;#第三页
select * from student limit 15 , 5;#第四页

注意:一般情况下:前端给我们提供参数pageSize(每页显示数量)

pageIndex(页码1 2 3 4 5)

sql 复制代码
select * from studnet limit pageSize offset (pageIndex-1)*pageSize;

7.链表查询

涉及到多张表

查询出每个学生的班级名称

SQL92语法:(不好)

select xxx from A表名,B表名 where 表连接条件 and 数据查询条件

提前使用where,将连接条件和查询条件放在一起

sql 复制代码
select s.name,c.class_name from student s,class c where s.class_num=c.class_num and name = '张三'

SQL99语法:

select 字段(必须标明来自于哪个表当中) from A表名称 join B表 on 表的连接条件

sql 复制代码
select s.name,c.class_name from student s join class c on s.class_num = c.class_num

8.连接方式

1.内连接 inner join 查询到的是两个表当中相交的部分

sql 复制代码
select s.*,c.class_name from student s inner join class c on s.class_num=c.class_num;

2.左连接 left join 显示左表当中的全部信息

sql 复制代码
select s.*,c.class_name from student s left join class c on s.class_num=c.class_num;

3.右链接 right join 显示右表当中的全部信息

sql 复制代码
select s.*,c.class_name from student s right join class c on s.class_num=c.class_num;

附件:

student:

class:

相关推荐
油丶酸萝卜别吃5 小时前
前端转全栈学习路线
前端·学习
GlueNa2SiO39 小时前
第十八章 Linux故障排查与恢复
linux·服务器·笔记·学习
weixin_431600449 小时前
NestJS 入门(3):Guard 如何挡住未登录请求?
前端·后端·学习·nest.js
灯澜忆梦9 小时前
【MySQL10】进阶篇 | 索引_#2性能优化
数据库·sql·mysql·性能优化
带娃的IT创业者9 小时前
DeepTutor:当 Agent-Native 架构撞上个性化学习的临界点
学习·架构·ai agent·大模型应用·个性化学习·教育技术·agent-native架构
xian_wwq10 小时前
【学习笔记】Context Engineering,AI Agent 真正的内存管理-4/16
笔记·学习·context
寒月小酒11 小时前
AnythingLLM 学习
学习
油丶酸萝卜别吃14 小时前
MySQL B+ 树查询全过程详解
数据库·mysql
大模型码小白14 小时前
【AI】一文讲清 RAG:从大模型局限到企业级知识库落地流程
人工智能·深度学习·学习
Exclusive_Cat14 小时前
MySQL回表机制解析与优化策略
数据库·sql