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:

相关推荐
q***92511 小时前
MySQL 启动失败 (code=exited, status=1FAILURE) 异常解决方案
数据库·mysql
拼好饭和她皆失1 小时前
C#学习入门
开发语言·学习·c#
LFly_ice2 小时前
学习React-22-Zustand
前端·学习·react.js
qq_5470261792 小时前
Docker 搭建单节点MySQL服务
mysql·adb·docker
q***3753 小时前
爬虫学习 01 Web Scraper的使用
前端·爬虫·学习
面向星辰3 小时前
sql通配符(大量查找搜索索引)
数据库·sql
斐硕人3 小时前
SQL滚动求和
数据库·sql·mysql·maxcompute
车端域控测试工程师4 小时前
Autosar网络管理测试用例 - TC003
c语言·开发语言·学习·汽车·测试用例·capl·canoe
hd51cc4 小时前
动态链接编程 学习笔记
笔记·学习