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:

相关推荐
于眠牧北2 天前
MySQL的锁类型,表锁,行锁,MVCC中所使用的临键锁
mysql
Turnip12024 天前
深度解析:为什么简单的数据库"写操作"会在 MySQL 中卡住?
后端·mysql
西岸行者5 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
加号35 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
シ風箏5 天前
MySQL【部署 04】Docker部署 MySQL8.0.32 版本(网盘镜像及启动命令分享)
数据库·mysql·docker
WeiXin_DZbishe5 天前
基于django在线音乐数据采集的设计与实现-计算机毕设 附源码 22647
javascript·spring boot·mysql·django·node.js·php·html5
tryCbest5 天前
数据库SQL学习
数据库·sql
悠哉悠哉愿意5 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
爱可生开源社区5 天前
MySQL 性能优化:真正重要的变量
数据库·mysql
别催小唐敲代码5 天前
嵌入式学习路线
学习