– 查询
– 查询所有字段
– select * from 表名;
select * from students;
-- 源生的形式
select students.* from students;
select python_info.students.* from students;
-- 什么时候不能够省略数据库名, 访问的数据表是是该数据库中的表的时候不能够省略
-- 什么情况下不能够省略表名: 当一个sql语句中出现了多个表的时候就不 建议 省略
-- 查询指定字段
-- select 列1,列2,... from 表名;
select name,age,gender from students;
-- 使用 as 给字段起别名
-- select 字段 as 名字.... from 表名;
select name, gender from students;
# 错误的 select name as '名字', gender as 性别 from students;
select name as 名字, gender as 性别 from students;
-- select 表名.字段 .... from 表名;
select students.name, students.age, students.gender from students;
-- 可以通过 as 给表起别名
-- select 别名.字段 .... from 表名 as 别名;
select s.name, s.age, s.gender from students as s;
-- 消除重复行
-- distinct 字段
-- 查询班级学生的性别
select gender from students;
-- 查询班级有多少中性别
select distinct gender from students;
– 条件查询 满足条件 就能够进入结果集
– 比较运算符
– >
– 查询大于18岁的信息
-- <
-- 查询小于18岁的信息
-- >=
-- <=
-- 查询小于或者等于18岁的信息
-- = 而不是 '=='
-- 查询年龄为18岁的所有学生的名字
select * from students where age = 18;
-- != 或者 <> 实际开发中 最好使用 ! 表示不等于
-- 逻辑运算符
-- and
-- 18岁以上的女性
select * from students where age > 18 and gender = '女';
-- or
-- 18以上或者身高超过180(包含)以上
select * from students where age > 18 or height >= 180.00;
-- not 非
-- 年龄不是18岁的学生
select * from students where age != 18;
select * from students where not age = 18;
-- 不在18岁以上的女性
select * from students where not age > 18 and gender = 2;
-- 小括号来提高优先级
select * from students where not (age > 18 and gender = 2);
-- 年龄是小于或者等于18 并且是女性
select * from students where age <= 18 and gender = 2;
-- 模糊查询
-- like
-- % 表示任意字符可有可无
-- 查询姓名中 以 "小" 开始的名字
select * from students where name like "小%";
-- _ 表示任意一个字符
-- 查询有2个字的名字
select * from students where name like "__";
-- 查询有3个字的名字
select * from students where name like "___";
-- rlike 正则 regex
-- match search findall r""
-- 查询以 周开始的姓名
select * from students where name rlike "^周.*";
select * from students where name rlike "^周(.*)伦$";
-- 范围查询
-- in表示在一个非连续的范围内
-- 查询 年龄为18、34的学生
select * from students where age = 18 or age = 34;
select * from students where age in (18, 34);
-- not in 不非连续的范围之内
-- 年龄不是 18、34岁的学生的信息
select * from students where not (age = 18 or age = 34);
select * from students where age not in (18, 34);
-- 年龄不是 18、34岁之间的信息
select * from students where age < 18 or age > 34;
-- 18 ~ 34
select * from students where not (age < 18 or age > 34);
-- between ... and ...表示在一个连续的范围内 两边都会包含
-- 查询 年龄在18到34之间的的信息
select * from students where age between 18 and 34;
-- not between ... and ...表示不在一个连续的范围内
-- 查询 年龄不在在18到34之间的的信息
select * from students where age not between 18 and 34;
# 错误select * from students where age not (between 18 and 34);
-- 空判断 null 不能够使用比较运算符
-- 查询身高为空的信息
select * from students where height is null;
-- 查询身高不为空的学生
select * from students where height is not null;
– 排序
– order by 字段 默认就是升序排序
– asc从小到大排列,即升序
-- 查询年龄在18到34岁之间的男性,按照年龄从小到大排序
select * from students where age between 18 and 40 and gender = 1 order by age; # 默认是升序排序
select * from students where age between 18 and 40 and gender = 1 order by age asc;
-- 降序 desc
-- desc从大到小排序,即降序
-- 查询年龄在18到34岁之间的女性,身高从高到矮排序
select * from students where age between 18 and 34 and gender = 2 order by height desc;
select * from students where gender = 2 and age between 18 and 34 order by height desc;
-- order by 多个字段
-- 查询年龄在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序
select * from students where gender = 2 and age between 18 and 34 order by height desc, age asc;
-- 查询年龄在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序, 如果年龄也相同那么按照id从大到小排序
select * from students where gender = 2 and age between 18 and 34 order by height desc, age asc, id desc;
-- 按照年龄从小到大、身高从高到矮的排序
select * from students order by age asc, height desc;
– 聚合函数
– 总数
– count
– 查询男性有多少人,女性有多少人
select count(*) from students where gender = 1;
select count(*) from students where gender = 2;
-- 最大值
-- max
-- 查询最大的年龄
select max(age) from students;
-- 查询女性的最高 身高
select max(height) from students where gender = 2;
# 错误: select name, max(height) from students where gender = 2
-- 静香,小乔 180
-- 最小值
-- min
select min(age) from students;
-- 求和
-- sum
-- 计算所有人的年龄总和
select sum(age) from students;
-- 计算平均年龄
select sum(age) / count(age) from students;
# select sum(age) / count(*) from students;
-- 平均值
-- avg
-- 计算平均年龄
select avg(age) from students;
-- 计算平均身高
select sum(height) / count(height) from students;
-- 四舍五入 round(123.23 , 1) 保留1位小数
-- 计算所有人的平均年龄,保留2位小数
select round(avg(age),2) from students;
-- 计算男性的平均身高 保留2位小数
select round(avg(age),2) from students where gender = 1;
– 分组
– group by 字段
– 查看有哪些存在性别
—- 查询所有的性别
select distinct gender from students;
– 按照性别分组
select gender from students group by gender;
-- 计算每种性别中的人数
select gender, count(*) from students group by gender;
-- group_concat(...)
# 错误的 select gender,name from students group by gender;
select gender,group_concat(name) from students group by gender;
-- 查询同种性别中的姓名和身高
select gender,group_concat(name,"-",height) from students group by gender;
-- 计算男性的人数
select count(*) from students where gender = 1;
select gender, count(*) from students where gender = 1 group by gender;
-- 使用having
-- 可以使用having 表示对于已经分组的数据做进一步的筛选
select gender, count(*) from students group by gender having gender = 1;
-- 除了男生以外的分组的人数
select gender, count(*) from students where gender != 1 group by gender;
-- 可以使用having 表示对于已经分组的数据做进一步的筛选
select gender, count(*) from students group by gender having gender != 1;
-- having 对于分组之后的数据 做进一步的筛选
-- 查询每种性别中的平均年龄avg(age)
select gender, avg(age) from students group by gender;
-- 查询每种性别中的平均年龄avg(age), 最大年龄,平均身高,最高身高
select gender ,avg(age),max(age), avg(height), max(height) from students group by gender;
-- 查询平均年龄超过30岁的性别,以及姓名
-- 先对性别分组 对于分组之后的数据 再判断哪个组的平均年龄大于30岁, 查询性别和对应的名字
select gender,group_concat(name) from students group by gender having avg(age) > 30;
-- having 和 where 的区别
where 是对于源数据的筛选操作, 满足条件就进入结果集合
having 是对于分组之后数据进行筛选, 有having 就一定有group by, 有group by 不一定有having
– 分页
– limit start, count
– start 默认值为0 可以省略
– start 表示略过多少条
select * from students limit 5;
-- 查询前5个数据
找规律:
已知当前用户点击的页码n(从1开始), 每页显示m条数据,求: 显示哪些数据
limit (n -1)* m,m
-- 每页显示2个,第1个页面
-- 每页显示2个,第2个页面
-- 每页显示2个,第3个页面
-- 每页显示2个,第4个页面
-- 每页显示2个,第5个页面
-- 每页显示2个,显示第6页的信息
-- 计算交给程序来完成 sql 专注于查询
# 错误select * from students limit (6 - 1)*2, 2;
select * from students limit 10, 2;
-- 每页显示2个,显示第6页的信息, 按照年龄从小到大排序
# 错误select * from students limit 10, 2 order by age asc;
-- 正确顺序
select * from students order by age asc limit 10,2;
– 连接查询 将两个表按照某种条件合并到一起
-- 查询学生的信息和学生对应的班级名字
-- 笛卡尔积
select students.*, classes.name from students,classes where students.cls_id=classes.id;
-- inner join ... on 内连接查询
-- 连接查询
select students.*, classes.name from students inner join classes on students.cls_id = classes.id;
select * from students inner join classes on students.cls_id = classes.id;
-- 按照要求显示姓名、和学生对应的班级的名字
select students.name, students.gender, classes.name from students inner join classes on students.cls_id = classes.id;
select s.name, s.gender, c.name from students as s inner join classes as c on s.cls_id = c.id;
-- 在以上的查询中,将班级名字显示在第1列
select c.name, s.name, s.gender from students as s inner join classes as c on s.cls_id = c.id;
-- 查询 有能够对应班级的学生以及班级信息, 按照班级进行排序
-- select c.xxx s.xxx from student as s inner join classes as c on .... order by ....;
select c.name, s.name, s.gender from students as s inner join classes as c on s.cls_id = c.id order by c.name;
-- left join 左外连接查询
-- 查询每位学生对应的班级信息
select s.name, s.gender, c.name from students as s left join classes as c on s.cls_id = c.id;
-- right join 右外连接查询 on 使用的比较少
-- 将数据表名字互换位置,用left join完成
select s.name, s.gender, c.name from students as s right join classes as c on s.cls_id = c.id;
# 如果使用 right join 可以 调换表的顺序 换成左外连接查询
select s.name, s.gender, c.name from classes as s left join students as c on s.cls_id = c.id;
-- inner join 的其他写法 join 或者 cross join
select s.name, s.gender, c.name from students as s join classes as c on s.cls_id = c.id;
select s.name, s.gender, c.name from students as s cross join classes as c on s.cls_id = c.id;
-- left join 可以写成 left outer join
select s.name, s.gender, c.name from classes as s left outer join students as c on s.cls_id = c.id;
– 自关联 自己关联自己
– 准备数据
create table areas(
aid int primary key,
atitle varchar(20),
pid int
);
– 省级联动 url:http://demo.lanrenzhijia.com/2014/city0605/
-- 查询所有省份
select * from areas where pid is null;
-- 查询出广东省有哪些市
广东省 广州市
广东省 深圳市
-- 发挥想象力: areas 表想象成两张表 --> 省表和市表
-- Column 'atitle' in where clause is ambiguous
select p.atitle, c.atitle from areas as p inner join areas as c on c.pid = p.aid where p.atitle = "广东省";
-- 查询出广州市有哪些区县
-- p: parent s: son
select p.atitle, s.atitle from areas as p inner join areas as s on s.pid = p.aid where p.atitle = "广州市";
-- 无限分级的业务场景
– 子查询 select …. select
– 子查询的语句 必须在小括号之内
– 查询学生的平均身高
-- 标量子查询 子查询得到的结构 是一行一列
-- 查询出高于平均身高的信息
-- 列级子查询
-- 查询哪些班级有学生
-- 子查询对应的关键字 in