一、数据准备、基本查询
- 创建数据库和数据表
-- 创建数据库
create database python_test_1 charset=utf8;
-- 使用数据库
use python_test_1;
-- students表
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default '',
age tinyint unsigned default 0,
height decimal(5,2),
gender enum('男','女','中性','保密') default '保密',
cls_id int unsigned default 0,
is_delete bit default 0
);
-- classes表
create table classes (
id int unsigned auto_increment primary key not null,
name varchar(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 students;
-- 查询指定字段
select name from students;
-- 使用as给字段起别名
select id as 序号, name as 名字, gender as 性别 from students;
-- 可以通过as给表起别名
select students.id, students.name, students.gender from students;
select s.id, s.name, s.gender from students as s;
-- 消除重复行
select distinct gender from students;
二、条件
- 优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
- and比or先运算,如果同时出现并希望先算or,需要结合()使用
-- 比较运算符
-- >, <, >=, <=, =, !=
-- 查询大于18岁的信息
select id,name,gender from students where age>18;
-- 逻辑运算符
-- and, or, not,
-- 年龄不是小于或者等于18 并且是女性
select * from where (not age<=18) and gender=2;
-- 模糊查询
-- like, rlike, %(1个或者多个), _(1个)
-- 查询至少有2个字的名字
select name from students where name like "__%";
-- 查询以 周开始、伦结尾的姓名
select name from students where rlike "^周.*伦$";
-- 范围查询
-- in, not in, between ... and ..., 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);
-- 空判断
-- is null, is not null
-- 身高为空的信息
select * from students where height is null;
三、排序
-- order by 字段 (asc或者desc), 默认asc
-- 查询年龄在18到34之间的男性,按照年龄从小到大排序
select * from students where (age between 18 and 34) and gender=1 order by age;
-- 查询年龄在18到34之间的女性,身高从高到矮排序,相同身高下按照年龄从小到大,年龄相同按照id从大到小排序 (多个字段排序)
select * from students where (age between 18 and 34) and gender=2 order by height desc,age asc,id desc;
四、聚合函数、分组
-- 聚合函数
-- count, max, min, sum, avg, round
-- 计算男性平均身高 保留2位小数
select name, round(avg(height),2) from students where gender=1;
-- 分组
-- group by, group concat(), having, with rollup(最后新增一行,记录当前列总和)
-- 计算男性的人数
select gender,count(*) from students where gender=1 group by gender;
-- 查询同种性别中的姓名
select gender,group_concat(name, "_", age, "_", id) from students where gender=1 group by gender;
-- 查询平均年龄超过30岁的性别,以及其中姓名
select gender, group_concat(name), avg(age) from students group by gender having avg(age)>30;
-- 查询各性别人数和总人数
selct gender, count(*), from students group by gender with rollup;
六、分页
-- 分页
-- limit start, count
-- 限制查询出来的数据个数
select * from students where gender=1 limit 2;
-- 查询前5个数据
select * from students limit 0, 5;
-- 每页显示2个,第4个页面(公式为:2*(n-1), 2)
select * from students limit 6, 2;
七、连接查询、自关联、子查询
-- 连接查询
-- inner join ... on
-- left join ... on
-- right join ... on (都可以由left join完成,互换位置)
-- 查询 有能够对应班级的学生以及班级信息
select * from students inner join classes on students.cls_id=classes.id;
-- 查询 没有对应班级信息的学生
select * from students left join classes on studetns.cls_id=classes.id where c.id is null;
-- 自关联
-- 省级联动 url:http://demo.lanrenzhijia.com/2014/city0605/
-- 查询湖南有那些市
select province.atitle, city.atitle from areas as province inner join areas as city on city.pid=province.aid having province.atitle='湖南省';
-- 子查询
-- 查询最高的男生信息
select * from students where height=(select max(height) from students);
完整sql语句
SELECT select_expr [,select_expr,...] [
FROM tb_name
[WHERE 条件判断]
[GROUP BY {col_name | postion} [ASC | DESC], ...]
[HAVING WHERE 条件判断]
[ORDER BY {col_name|expr|postion} [ASC | DESC], ...]
[ LIMIT {[offset,]rowcount | row_count OFFSET offset}]
]
-- 简化:
select distinct *
from 表名
where ....
group by ... having ...
order by ...
limit start,count
问题: windows下mysql的source areas.sql 出现报错 Incorrect string value,正常一条条添加没问题