> 文章列表 > SQL——数据查询DQL

SQL——数据查询DQL

SQL——数据查询DQL

基本语句、时间查询(当天、本周,本月,上一个月,近半年的数据)。

目录

1 查询语句基本结构

2 where 子句

条件关系运算符

4 条件逻辑运算符

5 like 子句

6 计算列

7 as 字段取别名

8 distinct 清除重复行

9 排序 order by

10 聚合函数 

10.1 count() 统计函数

10.2 max() 最大值函数

10.3 min() 最小值函数

10.4 avg() 平均值函数

11 日期函数

12 字符串函数

13 分组查询 group by

14 分页查询 limit

15 时间查询语句


1 查询语句基本结构

# 查询指定列
select colnumName1[,colnumName2,colnumName3...] from <tableName> [where
conditions];
# 查询到的记录的所有列,则可以使⽤ * 替代字段名列表
select * from <tableName>;

——查询全部列,也可指定某列

select s_num,s_name,s_sex,s_age,s_dept from student;select * from student;

where 子句

在删除、修改及查询的语句后都可以添加where⼦句(条件),⽤于筛选满⾜特定的添

加的数据进⾏删除、修改和查询操作。
delete from tableName where conditions;
update tabeName set ... where conditions;
select .... from tableName where conditions;

条件关系运算符

## = 等于
select * from student where s_num='230101';## != <> 不等于
select * from student where s_num !='230101';
select * from student where s_num <> '230101';## > ⼤于
select * from student where s_age>23;## < ⼩于
select * from student where s_age<23;## >= ⼤于等于
select * from student where s_age>=23;## <= ⼩于等于
select * from student where s_age<=23;## between and 区间查询 between v1 and v2 [v1,v2]
select * from student where s_age between 23 and 24;

条件逻辑运算符

# and 并且 筛选多个条件同时满⾜的记录
select * from student where s_age=23 and s_sex='女';# or 或者 筛选多个条件中⾄少满⾜⼀个条件的记录
select * from student where s_age=23 or s_sex='女';# not 取反
select * from student where s_age not between 22 and 24;

like 子句

# 查询学⽣姓名包含'张一'的学⽣信息
select * from student where s_name like '张一';# 查询学⽣姓名第⼀个字为'张'的学⽣信息
select * from stus where stu_name like '张%';# 查询学⽣姓名最后⼀个字⺟为'一'的学⽣信息
select * from student where s_name like '%一';# 查询学⽣姓名中第⼆个字为小的学⽣信息
select * from student where s_name like '_小%';

% 表示任意多个字符 【 %一% 包含字一】

_ 表示任意⼀个字符  【 _小% 第⼆个字为小】

计算列

对某列进行计算并显示。 

select s_name,2023-s_age from student;

7 as 字段取别名

 在上面计算的基础下,重新定义一个列名。

select s_name,2023-s_age as s_birthday from student;

distinct 清除重复行

从查询的结果中将重复的记录清除。
select s_age from student;

9 排序 order by

将查询到的满⾜条件的记录按照指定的列的值升序(asc)、降序(desc)排列。
select * from tableName where conditions order by columnName asc|desc;# 单字段排序
select * from student where s_age>22 order by s_sex desc;# 多字段排序
select * from student where s_age>22 order by s_sex asc,s_age desc;

10 聚合函数 

进⾏计算的函数count 、max 、min 、sum 、avg。

10.1 count() 统计函数

统计满足条件的指定字段值的个数(记录数) 。

# 统计学生表总数
select count(stu_num) from stus;# 统计学⽣表中性别为女的学⽣总数
select count(s_num) from student where s_sex='女';

10.2 max() 最大值函数

查询满足条件的记录中指定列的最大值。

select max(s_age) from student;select max(s_age) from student where s_sex='男';

10.3 min() 最小值函数

查询满足条件的记录中指定列的最小值。

select max(s_age) from student;select max(s_age) from student where s_sex='男';

10.4 avg() 平均值函数

查询满足条件的记录中计算指定列的平均值。

select avg(s_age) from student;select avg(s_age) from student where s_sex='男';

11 日期函数

  • 向日期类型的列添加数据时,可以通过字符串类型赋值(字符串的格式必须为 yyyy-MM-dd hh:mm:ss)
  • 获取当前系统时间添加到日期类型的列,可以使⽤ now() 或者 sysdate()
# 创建新列——时间
alter table student add date datetime;

12 字符串函数

 对字符串进⾏处理。

  • concat :concat(colnum1,colunm2,...) 拼接多列
  • upper:upper(column) 将字段的值转换成⼤写
  • lower:lower(column) 将指定列的值转换成⼩写
  • substring:substring(column,start,len) 从指定列中截取部分显示 start从1开始
select concat(s_name,'-',s_sex) from student;
select upper(s_name) from student;
select lower(s_name) from student;
select s_name,substring(s_dept,1,3) from student;

13 分组查询 group by

select 分组字段/聚合函数
from 表名
[where 条件]
group by 分组列名 [having 条件]
[order by 排序字段]
select 后通常显示分组字段和聚合函数(对分组后的数据进行统计、求和、平均值等) 语句执⾏属性:
  • 先根据where条件从数据库查询记录
  • group by对查询记录进⾏分组
  • 执⾏having对分组后的数据进⾏筛选
# 先对查询的学⽣信息按性别进⾏分组(分成了男、⼥两组),然后再分别统计每组学⽣的个数
select s_sex,count(s_num) from student group by s_sex;# 先对查询的学⽣信息按性别进⾏分组(分成了男、⼥两组),然后再计算每组的平均年龄
select s_sex,avg(s_age) from student group by s_sex;# 先对学⽣按年龄进⾏分组,然后统计各组的学⽣数量,还可以对最终的结果排序
select s_age,count(s_num) from student group by s_age order by s_age;# 查询所有学⽣,按年龄进⾏分组,然后分别统计每组的⼈数,再筛选当前组⼈数>1的组,再按年龄升序显示出来
select s_age,count(s_num) from student
group by s_age
having count(s_num)>1
order by s_age;# 查询性别为'女'的学⽣,按年龄进⾏分组,然后分别统计每组的⼈数,再筛选当前组⼈数>1的组,再按年龄升序显示出来
select s_age,count(s_num) from student
where s_sex='女'
group by s_age
having count(s_num)>1
order by s_age;

14 分页查询 limit

select ...
from ... 
where ...
limit param1,param2
param1 int :表示获取查询语句的结果中的第⼀条数据的索引(索引从0开始)
param2 int:表示获取的查询记录的条数(如果剩下的数据条数<param2,则返回剩下的所有记录)

15 时间查询语句

——今天

select * from 表名 where to_days(时间字段列名) = to_days(now());

——昨天

select * from <tablename> where to_days(now()) - to_days(时间字段名)=1;

——近7天

select * from 表名 where date_sub(curdate(),interval 7 day) < date(时间字段名);

——本周

select * from 表名 where yearweek(date_format(时间字段名,'%Y-%m-%d')) = yearweek(now());

——上周

select * from 表名 where yearweek(date_format(时间字段名,'%Y-%m-%d')) = yearweek(now())-1;

——近7天

select * from 表名 where date_sub(curdate(),interval 7 day) < date(时间字段名);

——近30天

select * from 表名 where date_sub(curdate(),interval 30 day) < date(时间字段名);

——本月

select * from 表名 where date_format(时间字段名, '%Y%m') = date_dormat(curdate(),'%Y%m');

——上个月

select * from 表名 where period_diff(date_format(now(),'%Y%M'), date_format(时间字段名,'%Y%m'))=1;

 ——本季度

select * from 表名 where quarter(时间字段名) =quarter(now());

——上季度

select * from 表名 where quarter(时间字段名)=quarter(date_sub(now(),interval 1 quarter));

——本年

select * from 表名 where year(时间字段名, '%Y%m') = year(now());

——上年

select * from 表名 where year(时间字段名) = year(date_sub(now(),interval 1 year));