MySQL
指定条件搜索表数据
select 字段(*) from 表名 where 条件;
where类似于pyton中的if
判断是否为空
is null 为空
is not null 不为空
# 示例
select * from shijian where y is null;
select * from shijian where y is not null;
逻辑判断
and 与(左右两边的条件都需要满足)
or 或(左右两边的条件只需要满足一个)
not 非(取反)
模糊查询
like:模糊查询的关键字,通常和通配符一起使用
通配符:
%: 任意数量的字符
_: 一个字符
#实列
select * from students where 进行模糊查询的字段
like 模糊查询的信息
insert into students
values('20180418','xi',18,'1990-01-01');
insert into students
values('20180419','xii',18,'1990-01-01');
# 想要查询students表中,名字中包含‘xi'的所有学员信息。
select * from students where name like 'xi%';
select * from students where name like 'xi_';
范围查询
in 在...里面(范围)
between.... and..... 在...与...之间
# 查询年龄为16,18,19,20的学员信息
select * from students where age
in(16,18,19,20);
# 查询年龄在16~21之间的学员信息
select * from students where age between 16 and
21;
排序
order by 排序(从小到大)
desc 倒叙(排序从大到小)
# 将查询数据按照年龄从小到大进行排序
select * from students order by age;
# 将查询数据按照年龄从大到小进行排序
select * from students order by age desc;
限制行数
limit 限制表查询的行数
limit x 展示 x 行数据(从第一条数据开始,包括第一 条)
limit x,y 展示 x 行(从0开始数)开始的 y 条数据
# 查看前三条数据:limit 多少行数据
select * from students limit 3;
# 从第4条数据开始输出3条数据: limit 第几行(从0开始
的),多少条数据
select * from students limit 3,3;
聚合函数
count() 统计
sum() 求和
avg() 平均值
max() 最大值
min() 最小值
语法:select 聚合函数(字段) from 表格;
# 统计表格行数
select count(*) from grades;
# 求students表中的年龄总和
select sum(age) from students;
# 求grades表中分数的平均值
select avg(grade) from grades;
# 求students表中的年龄平均值
select avg(age) from students;
# 求grades表中最高分
select max(grade) from grades;
# 求grades表中最低分
select min(grade) from grades;