DQL:数据查询语言
查询全表
select * from 表名;
查询指定列
select
列名
1,
列名
2,… from
表名
;
条件查询
select * from
表名
where
条件
;
注意:
条件查询就是在查询时给出
WHERE
子句,在
WHERE
子句中可以使用如下运算符及关键
字:
=
、
!=
、
<>
、
<
、
<=
、
>
、
>=
BETWEEN…AND (
等价
<=
和
>=)
IN(set) (
包含
)
IS NULL (
非空
)
AND (
逻辑与
)
OR (
逻辑或
)
NOT (
逻辑非
)
XOR (
逻辑异或
)
示例
-- 查询成绩小于80的学员
select * from stu where score < 80;
-- 查询成绩等于100的学员
select * from stu where score = 100;
-- 查询成绩在85~100的学员
select * from stu where math between 80 and 100;
-- 查询姓名叫做“张三”或者“李四”的所有学生信息。
select * from stu where name beteween "张三" and "李四";
-- 查询成绩不小于80的学员
select * from stu where not score < 80;
-- 查询姓名不叫做“张三”或者“李四”的所有学生信息。
select * from stu where name not beteween "张三" and "李四";
-- 查询姓名叫做“张三”或者“李四”的所有学生信息。
select * from stu where name in ("张三","李四");
-- 查询成绩小于0或大于100的学员
select * from stu where score < 0 or score > 100;
-- 查询性别为空的学员
select * from stu where sex IS NULL;
模糊查询
语法
当想查询姓名中包含
a
字母的学生时就需要使用模糊查询了。模糊查询需要使用关键字
LIKE
。
_:
任意一个字母
%:
任意
0~n
个字母
'
张
%'
示例
-- 查找姓名为3个字母的学生信息
select * from stu where name like '___';//注意是3个_,表示匹配3个字符
-- 查找以字母b开头的学生信息
select * from stu where name like 'b%';
正则表达式
MySQL
同样也支持其他正则表达式的匹配,
MySQL
中使用
REGEXP
操作符来进行正则
表达式匹配。
![](https://i-blog.csdnimg.cn/blog_migrate/7b4392d747e15add76a7f6b2168fa0e2.png)
示例
--
查找姓名以
l
开头且以
y
结尾的学生信息
select
*
from
stu
where
name regexp
'^l'
and
name regexp
'y$'
;
去重
关键字
:distinct
示例
--
查询
stu
表中
age
字段,剔除重复行
select distinct
age
from
stu;
计算列
对从数据表中查询的记录的列进行一定的运算之后显示出来
+,-,*,/,%
示例
--
出生年份
=
当前年份
-
年龄
select
stu_name,
2021
-stu_age
from
stus;
别名
如果在连接查询的多张表中存在相同名字的字段,我们可以使用
表名
.
字段名
来进行区
分,如果表名或字段名太长则不便于
SQL
语句的编写,我们可以使用数据表别名
示例
-- 字段起别名
select name AS 姓名 from stu;
-- 表名起别名
select s.name,s.sex from stu AS s;
-- AS可以省略
select name 姓名 from stu;
select s.name,s.sex from stu s;
排序order by
将查询到的满足条件的记录按照指定的列的值升序
/
降序排列
语法:
select * from
表名
where
条件
order by
列名
asc|desc;
order by
列名 表示将查询结果按照指定的列排序
asc 按照指定的列升序(默认)
desc 按照指定的列降序
示例:
# 单字段排序
select * from stu where age>15 order by score desc;
# 多字段排序:先满足第一个排序规则,当第一个排序的列的值相同时再按照第二个列的
规则排序
select * from stus where age>15 order by score asc,age desc;
聚合函数
聚合函数是用来做纵向运算的函数:
COUNT()
:统计指定列不为
NULL
的记录行数;
MAX()
:计算指定列的最大值,如果指定列是字符串类型,那么使用字符串排序运算;
MIN()
:计算指定列的最小值,如果指定列是字符串类型,那么使用字符串排序运算;
SUM()
:计算指定列的数值和,如果指定列类型不是数值类型,那么计算结果为
0
;
AVG()
:计算指定列的平均值,如果指定列类型不是数值类型,那么计算结果为
0
;
示例
-- 统计年龄大于20的学员人数
select count(*) as cnt from stu where age>20;
-- 统计学员的总年龄
select sum(age) from stu;
-- 统计学员的平均年龄以及总年龄
select sum(age),avg(age) from stu;
-- 统计学员的最低年龄以及最高年龄
select max(age),min(age) from stu;
-- 计算班级平均分
select avg(score) from stu;
分组查询
分组:就是将数据表中的记录按指定的列进行分组
语法
select 分组字段/聚合函数
from 表名
[where 条件]
group by 分组列名 [having 条件]
[order by 排序字段]
注意
语句执行顺序:
1,
先根据
where
条件从数据库查询记录
2,group by
对查询记录进行分组
3,
执行
having
对分组后的数据进行筛选
4,
排序
示例
-- 先对查询的学生信息按性别进行分组(分成了男、女两组),然后再分别统计每组
学生的个数
select stu_gender,count(stu_num) from stus group by stu_gender;
-- 先对查询的学生信息按性别进行分组(分成了男、女两组),然后再计算每组的平
均年龄
select stu_gender,avg(stu_age) from stus group by stu_gender;
-- 先对学生按年龄进行分组,然后统计各组的学生数量,还可以对最终的结果排序
select stu_age,count(stu_num) from stus group by stu_age order by
stu_age;
-- 查询所有学生,按年龄进行分组,然后分别统计每组的人数,再筛选当前组人数>1
的组,再按年龄升序显示出来
select stu_age,count(stu_num)
from stus
group by stu_age
having count(stu_num)>1
order by stu_age;
-- 查询性别为'男'的学生,按年龄进行分组,然后分别统计每组的人数,再筛选当前组人
数>1的组,再按年龄升序显示出来
select stu_age,count(stu_num)
-> from stus
-> where stu_gender='男'
-> group by stu_age
-> having count(stu_num)>1
-> order by stu_age;
分页查询
语法
select
查询的字段
from
表名
where
条件
limit
param1,param2;
注意
:
- param1
:表示获取查询语句的结果中的第一条数据的索引(索引从
0
开始)
- param2
:表示获取的查询记录的条数(如果剩下的数据条数
<param2
,则返回剩下的
所有记录)
注意
--
示例
--
假如
:
对数据表中的学生信息进行分页显示,总共有
10
条数据,我们每页显示
3
条
--
总记录数:
`count 10`
--
每页显示:
`pageSize 3`
--
总页数:
`pageCount=count%pageSize==0?
count/pageSize:count/pageSize+1`
--
查询第一页:
select
*
from
stus [
where
...]
limit
0
,
3
; (
1
-
1
)*
3
--
查询第二页:
select
*
from