group by 分页 mysql_MySQL 查询(条件、分组、聚合、分页、连接、子查询)

本文介绍了MySQL查询的基础知识,包括使用as和distinct、条件查询(如比较、逻辑、模糊、范围和空值判断)、排序(order by)、分组与聚合函数(如count、max、min、sum、avg及group by、having)以及分页(limit)。还讲解了连接查询(inner join、left join)、子查询和自关联的应用。
摘要由CSDN通过智能技术生成

MySQL查询 - as & distinct

-- 使用 as 给字段起别名

-- select 字段 as 字段别名 from 表名;

select name as 姓名, age as 年龄 from students;

-- 使用 as 给表起别名

-- select 表别名.字段 from 表名 as 表别名;

select students.name, students.age from students;

select s.name, s.age from students as s;

-- 使用 distinct 消除重复行

select distinct gender from students;

一、条件查询

比较运算符

>, <=, =, !=

select * from students where age>18;

select * from students where age=18;

逻辑运算符

and & or & not: 与或非

-- and & or

select * from students where age>18 and age<28;

select * from students where age>18 or height>=180;

-- not

select * from students where not (age>18 and gender=2);

select * from students where (not age<=18) and gender=2;

模糊查询

like: % 匹配1个或者多个,_ 匹配1个

rlike: 正则匹配

-- like

--

-- 查询姓名中 有 "小" 所有的名字

select name from students where name like "%小%";

-- 查询有2个字的名字

select name from students where name like "__";

-- 查询至少有2个字的名字

select name from students where name like "__%";

-- rlike 正则

select name from students where name rlike "^周.*伦$";

范围查询

in (a, b): 集合匹配

between and: 范围匹配

-- in (1, 3, 8) 非连续范围

select name,age from students where age in (12, 18, 34);

-- not in 非连续范围

select name,age from students where age not in (12, 18, 34);

-- between and 连续范围

select name, age from students where age between 18 and 34;

-- not between and 表示不在一个连续的范围内

select * from students where age not between 18 and 34;

select * from students where not age between 18 and 34;

-- 错误:select * from students where age not (between 18 and 34);

空判断

is null & is not null: 空 & 非空

-- null 或 Null 或 NULL 都行

select * from students where height is null

select * from students where height is not null;

二、排序 - order by [asc/desc]

-- select 默认以主键从小到大排序

-- order by 字段 [asc/desc]

select * from students where (age between 18 and 34) and gender=1;

select * from students where (age between 18 and 34) and gender=1 order by age;

select * from students where (age between 18 and 34) and gender=1 order by age asc;

-- order by 多个字段

-- 如果身高相同的情况下按照年龄从小到大排序,如果年龄也相同那么按照id从大到小排序

select * from students where gender=2 order by height desc,age asc,id desc;

三、分组与聚合函数

聚合函数 - count()/max()/min()/sum()/avg()

-- 总数-count()

select count(*) as 男性人数 from students where gender=1;

-- 最大最小值-max()/min()

select max(age) from students;

-- 求和-sum

select sum(age) from students;

-- 平均值-avg

select avg(age) from students;

综合使用:

-- 计算平均年龄 sum(age)/count(*)

select sum(age)/count(*) from students;

-- 四舍五入-round(x,n) 四舍五入x保留n位小数

-- 计算平均年龄,保留2位小数

select round(sum(age)/count(*), 2) from students;

select round(sum(age)/count(*), 3) from students;

-- 计算男性的平均身高 保留2位小数

select round(avg(height), 2) from students where gender=1;

-- 错误:select name, round(avg(height), 2) from students where gender=1;

分组 - group by [having]

分组和聚合函数一起用,语法:

select 字段a, 聚合函数()

from 表名

where 字段n 符合 条件

group by 字段a;

使用举例:

-- 显示一列所有的性别

select gender from students group by gender;

-- 计算男性的人数

select gender,count(*) from students where gender=1 group by gender;

-- 错误:select name from students group by gender;

-- 错误:select * from students group by gender;

group_concat()

-- 姓名按性别分组

select gender,group_concat(name) from students group by gender;

select gender,group_concat(name, " ", age, " ", id) from students where gender=1 group by gender;

having

where和having区别:where 是筛选数据来分组,having 是对分组后的数据进行筛选

-- 查询平均年龄超过30岁的姓名,并按性别分组

select gender, group_concat(name),avg(age) from students group by gender having avg(age)>30;

-- 查询人数多于2个的性别分组

select gender, group_concat(name) from students group by gender having count(*)>2;

四、分页 - limit

语法:limit 开始, 个数

-- 查询前5个数据

select * from students limit 5;

select * from students limit 0, 5;

-- 查询从第6个开始之后的总共10个数据(6-15)

select * from students limit 5, 10;

-- 不支持运算符,错误:select * from students limit 2*(6-1),2;

limit 必须要写在 sql 语句的最后

-- 错误:select * from students limit 10,2 order by age asc;

select * from students order by age asc limit 10,2;

五、连接查询

内连接:inner join on

若没有写 on 条件,classes 表的内容将顺序重复连接到 students 表

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

-- 按照要求显示姓名、班级

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

-- 查询有能够对应班级的学生的所有信息,并显示班级名称,并给数据表起别名

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

-- 在以上的查询中,将班级姓名显示在第1列

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

-- 查询有能够对应班级的学生以及班级信息, 按照班级进行排序

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

外连接:left join (& right join)

-- 查询每位学生对应的班级信息

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

-- 查询没有对应班级信息的学生

select * from students as s left join classes as c on s.cls_id=c.id having c.id is null;

自关联&子查询

自关联

-- 省份和城市的信息都存在同一张表,查山东省有哪些市

select province.name, city.name

from areas_tb as province

inner join areas_tb as city

on city.pid=province.id

having province.name="山东省";

-- 上面等价于下面这条子查询语句

select * from areas_tb where pid=(select id from areas_tb where name="山东省")

子查询

select * from students where height = (select max(height) from students);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值