查询相关
指定别名 as
select students.id,students.name from students
select s.id,s.name from students as s;
消除重复行
select distinct 列1,… from 表名
使用where字句对数据筛选
select * from 表名 where 条件
- 比较运算符
- 等于: =
- 大于: >
- 大于等于: >=
- 小于: <
- 小于等于: <=
不等于: != 或 <>
逻辑运算符
and
- or
- not
# 查询编号大于3的学生 select * from students where id>3; # 查询编号不大于4的学生 select * from students where id <=4; # 查询没有被删除的学生 select * from students where isdelete=0; # 查询编号大于3的女学生 select * from students where id >3 and gender=0; # 查询编号小于4或者没有被删除的学生 select * from students where id <4 or isdelete=0;
模糊查询
like
%表示任意多个任意字符
_表示一个任意字符
# 查询姓黄的学生 select * from students where name like '黄%'; # 查询姓黄并且名字是一个字的学生 select * from students where name like '黄_'; # 查询姓黄或叫婧的学生 select * from students where name like '黄%' or name like '%婧';
范围查询
in 表示在一个非连续的范围内
between … and … 表示在一个连续的范围内
# 查询编号是1或3或8的学生 select * from students where id in(1,3,8) # 查询编号为3至8的学生 select * from students where id between 3 and 8;
空判断
注意:null与”是不同的
判空is null
判非空is not null
# 查询没有填写身高的学生 select * from students where height is null; # 查询填写身高的学生 select * from students where height is not null;
优先级
- 优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
- and比or先运算,如果同时出现并希望先算or,需要结合()使用
排序
select * from 表名 order by 列1(字段) asc/desc,列2asc/desc
将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
默认按照列值从小到大排列
asc从小到大排列,即升序
desc从大到小排序,即降序
# 查询未删除男生信息,按学号降序 select * from students where gender=1 and isdelete=0 order by id desc;
聚合函数
count(*)表示计算总行数,括号中写星与列名,结果是相同的
聚合函数不能在 where 中使用
max(列)表示求此列的最大值
min(列)表示求此列的最小值
sum(列)表示求此列的和
avg(列)表示求此列的平均值
# 查询学生总数 select count(*) from students; # 查询女生的标号最大值 select max(id) from students where gender=0; # 查询删除学生的最小编号 select min(id) from students where isdelete=0; # 查询男生的编号之后 select sum(id) from students where gender=1; # 查询未删除女生的编号平均值 select avg(id) from students where isdelete=0 and gender=0;
分组
按照字段分组,表示此字段相同的数据会被放到一个组中
分组后,分组的依据列会显示在结果集中,其他列不会显示在结果集中
可以对分组后的数据进行统计,做聚合运算
select 列1,列2,聚合… from 表名 group by 列1,列2…
# 查询男女生总数 select gender as 性别,count(*) from students group by gender;
分组后数据筛选
select 列1,列2,聚合… from 表名 group by 列1,列2,列3… having 列1,… 聚合….
having后面条件运算符与where相同
select count(*) from students where gender=1;
获取部分行
select * from 表名 limit start,count
从start开始,获取count条数据
start索引从0开始
select * from students where gender=1 limit 0,3;
连接查询
- 当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回
mysql支持三种类型的连接查询,分别为:
内连接查询:查询的结果为两个表匹配到的数据
右连接查询:查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null填充
左连接查询:查询的结果为两个表匹配到的数据,左表特有的数据,对于右表中不存在的数据使用null填充
语法
select * from 表1
inner或left或right join 表2 on 表1.列=表2.列
- 例1:使用内连接查询班级表与学生表
- 此处使用了as为表起别名,目的是编写简单
select * from students inner join pythons on students.cls_id = pythons.id;
- 例2:使用左连接查询班级表与学生表
select * from students as s left join pythons as p on s.cls_id = p.id;
- 例3:使用右连接查询班级表与学生表
select * from students as s inner join pythons as p on s.cls_id = p.id;
- 例4:查询学生姓名及班级名称
select s.name,p.name from students as s inner join pythons as p on s.cls_id = p.id;
子查询
- 在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句
======__======= 未完待续