MySQL查询

1.条件查询

select * from 表名 where 条件;

where后面支持多种运算符,进行条件的处理

  • 比较运算符
  • 逻辑运算符
  • 模糊查询
  • 范围查询
  • 空判断

比较运算符

  • 等于: =
  • select * from students where is_delete=0;
  • 大于: >
  • select * from students where id > 3;
  • 大于等于: >=
  • 小于: <
  • 小于等于: <=
  • select * from students where id <= 4;
  • 不等于: != 或 <>
  • select * from students where name != '黄蓉';

逻辑运算符

  • and
  • select * from students where id > 3 and gender=0;
  • or
  • select * from students where id < 4 or is_delete=0;
  • not

模糊查询

  • like
  • select * from students where name like '黄%';
  • %表示任意多个任意字符
  • _表示一个任意字符
  • select * from students where name like '黄_';

范围查询

  • in表示在一个非连续的范围内
  • select * from students where id in(1,3,8);
  • between ... and ...表示在一个连续的范围内
  • select * from students where (id between 3 and 8) and gender=1;

空判断

  • 注意:null与''是不同的
  • 判空is null
  • select * from students where height is null;
  • 判非空is not null
  • select * from students where height is not null and gender=1;

优先级

  • 优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
  • and比or先运算,如果同时出现并希望先算or,需要结合()使用

2、排序

select * from 表名 order by 列1 asc|desc
说明
  • 将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
  • 默认按照列值从小到大排列(asc)
  • asc从小到大排列,即升序
  • desc从大到小排序,即降序

3、聚合函数

总数

  • count(*)表示计算总行数,括号中写星与列名,结果是相同的

例1:查询学生总数

select count(*) from students;

最大值

  • max(列)表示求此列的最大值

例2:查询女生的编号最大值

select max(id) from students where gender=2;

最小值

  • min(列)表示求此列的最小值

例3:查询未删除的学生最小编号

select min(id) from students where is_delete=0;

求和

  • sum(列)表示求此列的和

例4:查询男生的总年龄

select sum(age) from students where gender=1;

-- 平均年龄
select sum(age)/count(*) from students where gender=1;

平均值

  • avg(列)表示求此列的平均值

例5:查询未删除女生的编号平均值

select avg(id) from students where is_delete=0 and gender=2;

4、分组

group by

  1. group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组
  2. group by可用于单个字段分组,也可用于多个字段分组
select gender from students group by gender;

group by + group_concat()

  1. group_concat(字段名)可以作为一个输出字段来使用,
  2. 表示分组之后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合
select gender from students group by gender;
select gender,group_concat(name) from students group by gender;

group by + 集合函数

  1. 通过group_concat()的启发,我们既然可以统计出每个分组的某字段的值的集合,那么我们也可以通过集合函数来对这个值的集合做一些操作
select gender,group_concat(age) from students group by gender;
分别统计性别为男/女的人年龄平均值
select gender,avg(age) from students group by gender;

group by + having

  1. having 条件表达式:用来分组查询后指定一些条件来输出查询结果
  2. having作用和where一样,但having只能用于group by
select gender,count(*) from students group by gender having count(*)>2;

group by + with rollup

  1. with rollup的作用是:在最后新增一行,来记录当前列里所有记录的总和
select gender,count(*) from students group by gender with rollup;
select gender,group_concat(age) from students group by gender with rollup;

5、分页

语法

select * from 表名 limit start,count

说明

  • 从start开始,获取count条数据

例1:查询前3行男生信息

select * from students where gender=1 limit 0,3;

示例:分页

  • 已知:每页显示m条数据,当前显示第n页
  • 求总页数:此段逻辑后面会在python中实现
    • 查询总条数p1
    • 使用p1除以m得到p2
    • 如果整除则p2为总数页
    • 如果不整除则p2+1为总页数
  • 求第n页的数据
select * from students where is_delete=0 limit (n-1)*m,m

6、连接查询

当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回

mysql支持三种类型的连接查询,分别为:

  • 内连接查询:查询的结果为两个表匹配到的数据

  • 右连接查询:查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null填充

  • 左连接查询:查询的结果为两个表匹配到的数据,左表特有的数据,对于右表中不存在的数据使用null填充

语法

select * from 表1 inner或left或right join 表2 on 表1.列 = 表2.列

例1:使用内连接查询班级表与学生表

select * from students inner join classes on students.cls_id = classes.id;

例2:使用左连接查询班级表与学生表

  • 此处使用了as为表起别名,目的是编写简单
select * from students as s left join classes as c on s.cls_id = c.id;

例3:使用右连接查询班级表与学生表

select * from students as s right join classes as c on s.cls_id = c.id;

例4:查询学生姓名及班级名称

select s.name,c.name from students as s inner join classes as c on s.cls_id = c.id;

7、自关联

创建areas表的语句如下:

create table areas(
    aid int primary key,
    atitle varchar(20),
    pid int
);
  • 查询一共有多少个省

select count(*) from areas where pid is null;

  • 例1:查询省的名称为“山西省”的所有城市

select city.* from areas as city
inner join areas as province on city.pid=province.aid
where province.atitle='山西省';

  • 例2:查询市的名称为“广州市”的所有区县

select dis.* from areas as dis
inner join areas as city on city.aid=dis.pid
where city.atitle='广州市';
 

8、子查询

主查询和子查询的关系

  • 子查询是嵌入到主查询中
  • 子查询是辅助主查询的,要么充当条件,要么充当数据源
  • 子查询是可以独立存在的语句,是一条完整的 select 语句

子查询分类

  • 标量子查询: 子查询返回的结果是一个数据(一行一列)
  • 列子查询: 返回的结果是一列(一列多行)
  • 行子查询: 返回的结果是一行(一行多列)

标量子查询

  1. 查询班级学生平均年龄
  2. 查询大于平均年龄的学生

查询班级学生的平均身高

select * from students where age > (select avg(age) from students);

列级子查询

  • 查询还有学生在班的所有班级名字
    1. 找出学生表中所有的班级 id
    2. 找出班级表中对应的名字
select name from classes where id in (select cls_id from students);

行级子查询

  • 需求: 查找班级年龄最大,身高最高的学生
  • 行元素: 将多个字段合成一个行元素,在行级子查询中会使用到行元素
select * from students where (height,age) = (select max(height),max(age) from students);

子查询中特定关键字使用

  • in 范围
    • 格式: 主查询 where 条件 in (列子查询)

总结

  • 完整的select语句
select distinct *
from 表名
where ....
group by ... having ...
order by ...
limit start,count
  • 执行顺序为:
    • from 表名
    • where ....
    • group by ...
    • select distinct *
    • having ...
    • order by ...
    • limit start,count



 




posted @ 2019-05-05 22:28  阿磊小哥哥呀  阅读(165)  评论(0)    收藏  举报