查询基本使用(条件,排序,聚合函数,分组,分页)
--创建学生表
create table students (
id int unsigned not null
auto_increment primary key,
name varchar(20) default '',
age tinyint unsigned default 0,
high decimal(5,2),
gender enum('男', '女', '中性', '保密') default '保密',
cls_id int unsigned default 0,
is_delete bit default 0);
--创建班级表
create table classes(
id int unsigned auto_increment
primary key not null,
name varchar(20) not null);
--往students表里插入数据
insert into students values
(0,'小明',18,180.00,1,1,0),
(0,'小月月',19,180.00,1,2,0),
(0,'彭于晏',28,185.00,1,1,0),
(0,'刘德华',58,175.00,1,2,0),
(0,'黄蓉',108,160.00,2,1,0),
(0,'凤姐',44,150.00,4,2,1),
(0,'王祖贤',52,170.00,2,1,1),
(0,'周杰伦儿',34,null,1,1,0),
(0,'程坤',44,181.00,1,2,0),
(0,'和珅',55,166.00,1,2,0),
(0,'刘亦菲',29,162.00,2,3,0),
(0,'金星',45,180.00,3,4,0),
(0,'静香',18,170.00,2,4,0),
(0,'郭靖',22,167.00,1,5,0),
(0,'周杰',33,178.00,1,1,0),
(0,'钱小豪',56,178.00,1,1,0),
(0,'谢霆锋',38,175.00,1,1,0),
(0,'陈冠希',38,175.00,1,1,0);
查询
-- 查询所有列
--select * from 表名
select * from students;
--一定条件查询(where)
select * from students where id=8;
-- 查询制定列
select id,name,age from students;
-- 使用as给字段起别名
select id,age as '年龄',high, gender from students;
-- 通过表名字段查询
select students.id,students.name from students;
-- 给表起别名查询
select s.id,s.name,s.age from
students as s;
--消除重复行
-- distinct
select distinct high from students;
条件查询
--比较运算符
-- 查询年纪大于28岁的信息
select * from students where age
> 28;
--19岁到38岁之间(and)
select * from students where age
>= 19 and age <= 38;
select * from students where age between 19 and 38;
-- between 表示19到38,包括19和38
--在28岁以上或者身高185以上的人(or)
select * from students where age
> 28 or high > 185;
模糊查询
-- like
-- % 替代1个或者多个甚至是没有
-- 查询姓名中有‘豪’的所有名字
select * from students where name
like '%豪%';
-- 查询三个字人的名字
select * from students where name like '___';
-- 查询至少有三个字的名字
select * from students where name like '%___%';
范围查询
-- in (1,3,8)表示在一个非连续的范围内
-- 查询 年纪为18和34的人
select * from students where age
in (18, 34);
--in 后面括号中用逗号分隔开的数字表示单个数字,不代表范围,可以用逗号分隔添加多个数值,列如(15,25,35,56):表示一个15,一个25,一个35,一个56
--查询 年龄在19岁到37岁之间的信息
select * from students where age
between 19 and 37;
--查询 年纪不在19到37岁的信息
select * from students where age not between 18 and 36;
空判断
-- 判断is null
-- 查询身高为空的信息
select * from students where high
is null;
-- 判断非空is not null
select * from students where high is not null;
排序
-- order by 字段
-- asc从小到大排列,即升序
-- desc从大到小排序,即降序
-- 查询年纪在20到36岁之间的男性,按照年纪从小到大
select * from students where
gender=1 and age between 20 and 36
order by age;
-- order by 多字段
-- 查询年纪在18到34岁的男性,身高从高到矮排序,如果身高相同的情况下按照年纪从小到大排序
select * from students where age between 18
and 34 and gender=1 order by high desc;
-- 查询年纪在28到35岁的男性,身高从高到矮排序,如果身高相同的情况下按照年纪从小到大排序,如果年龄也相等那么按照id从小到大排序;
select * from students where age between 28 and 35 and gender=1 order by high desc, age, id desc;
聚合函数
-- 总数
-- count(*) 用*统计是最准确的,也可以在括号里加入某个字段,可能会不准确,最好使用*
-- 查询女性有多少人
select count(*) from students where
gender=2;
-- 最大值
-- max
-- 查询最高的身高
select max(high) from students;
-- 查询男性的最高大年龄
select max(age) from students where gender=1;
-- 最小值
-- min
--查看最小的年龄
select min(age) from students;
-- 求和
-- sum
-- 计算所有人的身高总和
select sum(high) from students;
-- 平均值
-- avg
-- 计算平均年纪
-- 计算平均年纪
sum(age)/count(*)
select sum(age)/count(*) from
students;
-- 保留3位小数
select round(avg(age),3) from students;
分组
-- group by
-- 按照性别分组,查询所有的性别
select gender from students group by
gender;
-- 计算每组性别的人数
select gender, count(*) from students group by gender;
-- 查询女性组中的姓名 group_concat() 按组显示括号中的内容
select gender,group_concat(name) from students where gender=2 group by gender;
-- having
-- 查询每个性别平均年纪超过40岁的性别,以及姓名 having avg(age) > 30
select gender, group_concat(name)
from students group by gender having avg(age) > 40;
-- 查询每种性别中的人数多于4个的组的信息
select gender,group_concat(name) from
students group by gender having count(*)>4;
分页
-- 显示6页
select * from students limit 6;
-- 分页显示,每页显示2条数据
select * from students limit 0, 2;
select * from students limit 1, 2;
select * from students limit 2, 2;
-- 按照身高从高到矮排序,查找出所有女性,并且分页显示,每页显示3条数据
select * from students where gender=2 order by high desc limit 0,3;
select * from students where gender=2 order by high desc limit 1,3;
select * from students where gender=2 order by high desc limit 2,3;