--创建数据库
create database python_test_1 charset=utf8;--使用数据库
usepython_test_1;--students表
create tablestudents(
idint unsigned primary key auto_increment not null,
namevarchar(20) default '',
agetinyint unsigned default 0,
heightdecimal(5,2),
gender enum('男','女','中性','保密') default '保密',
cls_idint unsigned default 0,
is_deletebit default 0);--classes表
create tableclasses (
idint unsigned auto_increment primary key not null,
namevarchar(30) not null);--向students表中插入数据
insert into students values(0,'小明',18,180.00,2,1,0),
(0,'小月月',18,180.00,2,2,1),
(0,'彭于晏',29,185.00,1,1,0),
(0,'刘德华',59,175.00,1,2,1),
(0,'黄蓉',38,160.00,2,1,0),
(0,'凤姐',28,150.00,4,2,1),
(0,'王祖贤',18,172.00,2,1,1),
(0,'周杰伦',36,NULL,1,1,0),
(0,'程坤',27,181.00,1,2,0),
(0,'刘亦菲',25,166.00,2,2,0),
(0,'金星',33,162.00,3,3,1),
(0,'静香',12,180.00,2,4,0),
(0,'郭靖',12,170.00,1,4,0),
(0,'周杰',34,176.00,2,5,0);--向classes表中插入数据
insert into classes values (0, "python_01期"), (0, "python_02期");--select * from 表名;--例:
select * fromstudents;--select 列1,列2,... from 表名;--例:
select name fromstudents;--使用 as 给字段起别名
select id as 序号, name as 名字, gender as 性别 fromstudents;--可以通过 as 给表起别名
--如果是单表查询 可以省略表明
select id, name, gender fromstudents;--表名.字段名
select students.id,students.name,students.gender fromstudents;--可以通过 as 给表起别名
select s.id,s.name,s.gender from students ass;--消除重复行--在select后面列前使用distinct可以消除重复的行--select distinct 列1,... from 表名;--例:
select distinct gender fromstudents;--where后面支持多种运算符,进行条件的处理--比较运算符--逻辑运算符--模糊查询--范围查询--空判断
--比较运算符查询--等于: =--大于: >--大于等于: >=--小于:
select * from students where id=1;select * from students where id > 3;select * from students where is_delete=0;--逻辑运算符查询--and--or--not
select * from students where id > 3 and gender=1;--模糊查询--like--%表示任意多个任意字符--_表示一个任意字符
select * from students where name like '黄%';select * from students where name like '黄_';select * from students where name like '黄%' or name like '%靖';--rlike 正则
select * from students where name rlike '黄.|.靖';select * from students where name rlike '.*蓉.*|.*菲$'
--范围查询--in表示在一个非连续的范围内--between ... and ...表示在一个连续的范围内
select * from students where id in(1,3,8);select * from students where id between 3 and 8;select * from students where (id between 3 and 8) and gender=1;--空判断--注意:null与''是不同的--判空is null--判非空is not null
select * from students where height is null;select * from students where height is not null and gender=1;--优先级--优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符--and比or先运算,如果同时出现并希望先算or,需要结合()使用
--排序--select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]--将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推--默认按照列值从小到大排列(asc)--asc从小到大排列,即升序--desc从大到小排序,即降序
select * from students where gender=1 and is_delete=0 order by id desc;select * from students order by age desc,height desc;--聚合函数--总数--count(*)表示计算总行数,括号中写星与列名,结果是相同的
select count(*) fromstudents;--最大值--max(列)表示求此列的最大值
select max(id) from students where gender=2;--最小值--min(列)表示求此列的最小值
select min(id) from students where is_delete=0;--求和--sum(列)表示求此列的和
select sum(age) from students where gender=1;--平均年龄
select sum(age)/count(*) from students where gender=1;--平均值--avg(列)表示求此列的平均值
select avg(id) from students where is_delete=0 and gender=2;--分组--group by--group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组--group by可用于单个字段分组,也可用于多个字段分组
select gender from students group bygender;--group by + group_concat()--group_concat(字段名)可以作为一个输出字段来使用,--表示分组之后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合
select gender,group_concat(name) from students group bygender;--group by + 集合函数--通过group_concat()的启发,我们既然可以统计出每个分组的某字段的值的集合,那么我们也可以通过集合函数来对这个值的集合做一些操作
select gender,group_concat(age) from students group bygender;--group by + having--having 条件表达式:用来分组查询后指定一些条件来输出查询结果--having作用和where一样,但having只能用于group by
select gender,count(*) from students group by gender having count(*)>2;--group by + with rollup--with rollup的作用是:在最后新增一行,来记录当前列里所有记录的总和
select gender,count(*),group_concat(age) from students group by gender withrollup;--获取部分行--select * from 表名 limit start,count--从start开始,获取count条数据
select * from students where gender=1 limit 0,3;--分页--select * from students where is_delete=0 limit (n-1)*m,m
select * from students where is_delete=0 limit 0,5
select * from students where is_delete=0 limit 5,5
--连接查询--mysql支持三种类型的连接查询,分别为:--内连接查询:查询的结果为两个表匹配到的数据--右连接查询:查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null填充--左连接查询:查询的结果为两个表匹配到的数据,左表特有的数据,对于右表中不存在的数据使用null填充--语法--select * from 表1 inner或left或right join 表2 on 表1.列 = 表2.列
select * from students inner join classes on students.cls_id =classes.id;select s.name,c.name from students as s left join classes as c on s.cls_id =c.id;--自关联
create tableareas(
aidint primary key,
atitlevarchar(20),
pidint);--从sql文件中导入数据
source areas.sql;select count(*) from areas where pid is null;select city.* from areas ascityinner join areas as province on city.pid=province.aidwhere province.atitle='山西省';select dis.* from areas asdisinner join areas as city on city.aid=dis.pidwhere city.atitle='广州市';--子查询--在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句--主查询:主要查询的对象,第一条 select 语句--主查询和子查询的关系--子查询是嵌入到主查询中--子查询是辅助主查询的,要么充当条件,要么充当数据源--子查询是可以独立存在的语句,是一条完整的 select 语句--子查询分类--标量子查询: 子查询返回的结果是一个数据(一行一列)--列子查询: 返回的结果是一列(一列多行)--行子查询: 返回的结果是一行(一行多列)--标量子查询--查询班级学生平均年龄--查询大于平均年龄的学生
select * from students where age > (select avg(age) fromstudents);--列级子查询--查询还有学生在班的所有班级名字--找出学生表中所有的班级 id--找出班级表中对应的名字
select name from classes where id in (select cls_id fromstudents);--行级子查询--需求: 查找班级年龄最大,身高最高的学生--行元素: 将多个字段合成一个行元素,在行级子查询中会使用到行元素
select * from students where (height,age) = (select max(height),max(age) fromstudents);--子查询中特定关键字使用--in 范围--格式: 主查询 where 条件 in (列子查询)