##数据准备、基本查询
2018年11月28日
创建数据库
create database 数据库名字;
create database python_test charset= utf8;
使用数据库
use python_test;
显示使用当前数据库是哪个
select database();
创建一个数据表
students表
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 # bit 一般是1 是包含0,1 bit 2 是0,1,2,3
);
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期”),(0,“python_04期”); 0 是代表让数据库自动填写增长序列号
查询
查询所有字段
select * from 表名;
select * from students;
select * from classes;
select id,name from classes;
查询指定字段
select 列1,列2,……. from 表名;
select name,age from students;
使用as起别名
select 字段 as 名字…. from 表名;
select name as 姓名,age as 年龄 from students;
select 表名.字段 from 表名;
select students.name,students.age from students;
select s.name,s.age from students as s;
distinct 去重 不管再多数据也显示不相同的一次输出
select distinct gender from students;
比较运算符
select …… from 表名 where …….;
查询大于18岁的信息
select * from students where age>18;
select id,name,gender from students where age>=18;
select id,name,gender,age from students where age>=18; --age 写不写都可以
查询小于 18岁的信息
select * from students where age<18;
查询小于或等于18岁的信息
select * from students where age<=18;
查询年龄为18岁的所有学生名字
select * from students where age=18;
!= 或者 <>
select * from students where age!=18;
逻辑运算符
and
18到28 之间 的所有学生信息
select * from students where age>18 and age<28; – 左右两边都是具体的,不能只有一边是具体的
mysql> select * from students where age>18 and age<28;
±—±----------±-----±-------±-------±-------±----------+
| id | name | age | high | gender | cls_id | is_delete |
±—±----------±-----±-------±-------±-------±----------+
| 9 | 程坤 | 27 | 181.00 | 男 | 2 | |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | |
±—±----------±-----±-------±-------±-------±----------+
失败 select * from students where age>18 and <28; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<28' at line 1
18 岁以上的女性
select * from students where age>18 and gender="女";
select * from students where age>18 and gender=2;
mysql> select * from students where age>18 and gender=“女”;
±—±----------±-----±-------±-------±-------±----------+
| id | name | age | high | gender | cls_id | is_delete |
±—±----------±-----±-------±-------±-------±----------+
| 5 | 黄蓉 | 38 | 160.00 | 女 | 1 | |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | |
| 14 | 周杰 | 34 | 176.00 | 女 | 5 | |
±—±----------±-----±-------±-------±-------±----------+
3 rows in set (0.00 sec)
or
18 岁以上或者身高超过180(包含)以上
select * from students where age>18 or high>=180;
mysql> select * from students where age>18 or high>=180;
±—±----------±-----±-------±-------±-------±----------+
| id | name | age | high | gender | cls_id | is_delete |
±—±----------±-----±-------±-------±-------±----------+
| 1 | 小明 | 18 | 180.00 | 女 | 1 | |
| 2 | 小月月 | 18 | 180.00 | 女 | 2 | |
| 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | |
| 4 | 刘德华 | 59 | 175.00 | 男 | 2 | |
| 5 | 黄蓉 | 38 | 160.00 | 女 | 1 | |
| 6 | 凤姐 | 28 | 150.00 | 保密 | 2 | |
| 8 | 周杰伦 | 36 | NULL | 男 | 1 | |
| 9 | 程坤 | 27 | 181.00 | 男 | 2 | |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | |
| 11 | 金星 | 33 | 162.00 | 中性 | 3 | |
| 12 | 静香 | 12 | 180.00 | 女 | 4 | |
| 14 | 周杰 | 34 | 176.00 | 女 | 5 | |
±—±----------±-----±-------±-------±-------±----------+
12 rows in set (0.01 sec)
not
不在 18 岁以上的女性 这个范围内的信息
select * from students where not age=18 and gender=2; --这个只是否定了age 这个条件,需要加括号才可以全部否定
select * from students where not (age=18 and gender=2);
年龄不是小于或者等于18 并且是女生
select * from students where (not age=18) and gender=2;
模糊查询
like
% 替换一个或者多个
_替换一个
查询姓名中 以“小”开始的名字
select name from students where name like “小”;
mysql> select name from students where name like “小”;
Empty set (0.00 sec)
select name from students where name like "小%";
mysql> select name from students where name like “%小%”;
±----------+
| name |
±----------+
| 小明 |
| 小月月 |
±----------+
2 rows in set (0.00 sec)
查询所有名字中带“小”的名字
select name from students where name like "%小%";
-- 查询有两个字的名字
select name from students where name like "__";
-- 查询有三个字的名字
select name from students where name like "___";
-- 查询至少有两个字的名字
select name from students where name like "__%";
-- rlike 正则
-- 查询 以 周开始的姓名
select name from students where name rlike "^周.*";
-- 查询以周开始 伦结尾的姓名
select name from students where name rlike "^周.*伦$";
-- 范围查询
-- in(1,3,8)表示一个非连续的范围
-- 查询 年龄为 18 、34 的姓名
-- select name from students where age=18 or age=34; -- 可以无限加or 但是很麻烦
select name,age from students where age in (12,18,34);
-- not in 不非连续的范围之内
-- 年龄不是 18 、34岁之间的信息
select name,age from students where age not in (12,18,34);
-- between … and … 表示在一个连续的范围内
-- 查询 年龄在18到34 之间的信息
select name,age 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 not age between 18 and 34; 不推荐
-- 失败 select * from students where age not (between 18 and 34);
-- 空判断
-- 判断 is null
-- 查询身高为空的信息
select * from students where high is null;
-- 判非空 is not null
select * from students where high is not null;
-- 排序
-- order by 字段
-- asc 从小到大排列, 即升序
-- desc 从大到小排列, 即降序
查询年龄在18 到34 岁之间的男性,按照年龄从小到大排序
select * from students where (age between 18 and 34) and gender=1;
mysql> select * from students where (age between 18 and 34) and gender=1;
±—±----------±-----±-------±-------±-------±----------+
| id | name | age | high | gender | cls_id | is_delete |
±—±----------±-----±-------±-------±-------±----------+
| 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | |
| 9 | 程坤 | 27 | 181.00 | 男 | 2 | |
±—±----------±-----±-------±-------±-------±----------+
select * from students where (age between 18 and 34) and gender=1 order by age;
mysql> select * from students where (age between 18 and 34) and gender=1 order by age;
±—±----------±-----±-------±-------±-------±----------+
| id | name | age | high | gender | cls_id | is_delete |
±—±----------±-----±-------±-------±-------±----------+
| 9 | 程坤 | 27 | 181.00 | 男 | 2 | |
| 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | |
±—±----------±-----±-------±-------±-------±----------+
2 rows in set (0.00 sec)
-- 查询年龄在18到34 岁之间的女性,身高从高到矮的排序
select * from students where (age between 18 and 34) and gender=2 order by high desc;
mysql> select * from students where (age between 18 and 34) and gender=2 order by high desc;
±—±----------±-----±-------±-------±-------±----------+
| id | name | age | high | gender | cls_id | is_delete |
±—±----------±-----±-------±-------±-------±----------+
| 1 | 小明 | 18 | 180.00 | 女 | 1 | |
| 2 | 小月月 | 18 | 180.00 | 女 | 2 | |
| 14 | 周杰 | 34 | 176.00 | 女 | 5 | |
| 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | |
±—±----------±-----±-------±-------±-------±----------+
5 rows in set (0.00 sec)
-- order by 多个字段
-- 查询年龄在18到34岁之间的女性,身高从高到矮排序,如果身高相同的情况下按照年龄从小到大排序
select * from students where (age between 18 and 34) and gender=2 order by high desc,age desc;
mysql> select * from students where (age between 18 and 34) and gender=2 order by high desc,age desc;
±—±----------±-----±-------±-------±-------±----------+
| id | name | age | high | gender | cls_id | is_delete |
±—±----------±-----±-------±-------±-------±----------+
| 1 | 小明 | 18 | 180.00 | 女 | 1 | |
| 2 | 小月月 | 18 | 180.00 | 女 | 2 | |
| 14 | 周杰 | 34 | 176.00 | 女 | 5 | |
| 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | |
±—±----------±-----±-------±-------±-------±----------+
5 rows in set (0.00 sec)
-- 查询年龄在18到34岁之间的女性,身高从高到矮拍讯,如果身高相同的情况下按照年龄从小到大排序,
-- 如果年龄也相同那么按照id 从小到大排序
select * from students where (age between 18 and 34) and gender=2 order by high desc,age desc,id desc;
mysql> select * from students where (age between 18 and 34) and gender=2 order by high desc,id desc;
±—±----------±-----±-------±-------±-------±----------+
| id | name | age | high | gender | cls_id | is_delete |
±—±----------±-----±-------±-------±-------±----------+
| 2 | 小月月 | 18 | 180.00 | 女 | 2 | |
| 1 | 小明 | 18 | 180.00 | 女 | 1 | |
| 14 | 周杰 | 34 | 176.00 | 女 | 5 | |
| 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | |
±—±----------±-----±-------±-------±-------±----------+
5 rows in set (0.00 sec)
-- 如果按照年龄从小到大、身高从高到矮的排序
select * from students order by age asc,high desc;
±—±----------±-----±-------±-------±-------±----------+
| id | name | age | high | gender | cls_id | is_delete |
±—±----------±-----±-------±-------±-------±----------+
| 12 | 静香 | 12 | 180.00 | 女 | 4 | |
| 13 | 郭靖 | 12 | 170.00 | 男 | 4 | |
| 1 | 小明 | 18 | 180.00 | 女 | 1 | |
| 2 | 小月月 | 18 | 180.00 | 女 | 2 | |
| 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | |
| 9 | 程坤 | 27 | 181.00 | 男 | 2 | |
| 6 | 凤姐 | 28 | 150.00 | 保密 | 2 | |
| 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | |
| 11 | 金星 | 33 | 162.00 | 中性 | 3 | |
| 14 | 周杰 | 34 | 176.00 | 女 | 5 | |
| 8 | 周杰伦 | 36 | NULL | 男 | 1 | |
| 5 | 黄蓉 | 38 | 160.00 | 女 | 1 | |
| 4 | 刘德华 | 59 | 175.00 | 男 | 2 | |
±—±----------±-----±-------±-------±-------±----------+
14 rows in set (0.00 sec)
mysql> select count(*) as 女性人数 from students where gender=2;
±-------------+
| 女性人数 |
±-------------+
| 7 |
±-------------+
1 row in set (0.00 sec)
最大值
max
查询最大的年龄
select max(age) from students;
mysql> select max(age) from students;
±---------+
| max(age) |
±---------+
| 59 |
±---------+
1 row in set (0.00 sec)
-- 查询女性的最高身高
select max(high) from students where gender=2;
-- 最小值
-- min
-- 求和
-- sum
-- 计算所有人的年龄总和
select sum(age) from students;
mysql> select sum(age) from students;
±---------+
| sum(age) |
±---------+
| 387 |
±---------+
1 row in set (0.00 sec)
-- 平均值
-- avg
-- 计算平均年龄
select avg(age) from students;
mysql> select avg(age) from students;
±---------+
| avg(age) |
±---------+
| 27.6429 |
±---------+
1 row in set (0.00 sec)
-- 计算平均年龄 sum(age)/count(*)
select sum(age)/count(*) from students;
mysql> select sum(age)/count() from students;
±------------------+
| sum(age)/count() |
±------------------+
| 27.6429 |
±------------------+
1 row in set (0.00 sec)
-- 四舍五入 round(123.23,1) 保留一位小数
-- 计算所有人的平均年龄,保留两位小数
select round(sum(age)/count(*),2) from students;
mysql> select round(sum(age)/count(),2) from students;
±---------------------------+
| round(sum(age)/count(),2) |
±---------------------------+
| 27.64 |
±---------------------------+
1 row in set (0.00 sec)
-- 分组
-- group by
-- 按照性别分组,查询所有性别
select name from students group by gender;
select gender from students group by gender;
-- 失败 select * from students group by gender;
-- 计算 每种性别中的人数
select gender,count(*) from students group by gender;
mysql> select gender,count() from students group by gender;
±-------±---------+
| gender | count() |
±-------±---------+
| 男 | 5 |
| 女 | 7 |
| 中性 | 1 |
| 保密 | 1 |
±-------±---------+
4 rows in set (0.00 sec)
-- 计算男性的人数
select gender,count(*) from students where gender=1 group by gender;
mysql> select gender,count() from students where gender=1 group by gender;
±-------±---------+
| gender | count() |
±-------±---------+
| 男 | 5 |
±-------±---------+
1 row in set (0.00 sec)
select gender,group_concat(name,"-",age,"-"id) from students where gender=1 group by gender;
mysql> select gender,group_concat(name,"-",age,"-",id) from students where gender=1 group by gender;
±-------±----------------------------------------------------------------------+
| gender | group_concat(name,"-",age,"-",id) |
±-------±----------------------------------------------------------------------+
| 男 | 彭于晏-29-3,刘德华-59-4,周杰伦-36-8,程坤-27-9,郭靖-12-13 |
±-------±----------------------------------------------------------------------+
1 row in set (0.00 sec)
-- group_concat(…)
-- 查询同种性别中的名字
select gender,group_concat(name) from students group by gender;
mysql> select gender,group_concat(name) from students group by gender;
±-------±----------------------------------------------------------+
| gender | group_concat(name) |
±-------±----------------------------------------------------------+
| 男 | 彭于晏,刘德华,周杰伦,程坤,郭靖 |
| 女 | 小明,小月月,黄蓉,王祖贤,刘亦菲,静香,周杰 |
| 中性 | 金星 |
| 保密 | 凤姐 |
±-------±----------------------------------------------------------+
4 rows in set (0.00 sec)
-- having
-- 查询 平均年龄超过30 岁的性别 ,以及姓名 having avg(age) > 30;
having 是对查找出来的结果做出判断,where是对表里面的数据进行判断
select gender, group_concat(name),avg(age) from students group by gender having avg(age)>30;
mysql> select gender, group_concat(name),avg(age) from students group by gender having avg(age)>30;
±-------±--------------------------------------------±---------+
| gender | group_concat(name) | avg(age) |
±-------±--------------------------------------------±---------+
| 男 | 彭于晏,刘德华,周杰伦,程坤,郭靖 | 32.6000 |
| 中性 | 金星 | 33.0000 |
±-------±--------------------------------------------±---------+
2 rows in set (0.01 sec)
-- 查询每种性别中的人数多于两个的信息
select gender, group_concat(name),avg(age) from students group by gender having count(*)>2;
mysql> select gender, group_concat(name),avg(age) from students group by gender having count(*)>2;
±-------±----------------------------------------------------------±---------+
| gender | group_concat(name) | avg(age) |
±-------±----------------------------------------------------------±---------+
| 男 | 彭于晏,刘德华,周杰伦,程坤,郭靖 | 32.6000 |
| 女 | 小明,小月月,黄蓉,王祖贤,刘亦菲,静香,周杰 | 23.2857 |
±-------±----------------------------------------------------------±---------+
2 rows in set (0.00 sec)
-- 分页
-- limit start ,count
-- 限制查询出来的数据个数
select * from students limit 5;
-- 查询前 5个数据
select * from students limit 0,5; -- 从0开始的5个数据
-- 查询 id 6-10(包含)的书序
-- 每页显示2个, 第1个页面
select * from students limit 0,2;
-- 每页显示2个,第2个页面
select * from students limit 2,2;
-- 每页显示2个,第3个页面
select * from students limit 4,2;
-- 每页显示2个,第4个页面
select * from students limit 6,2; ----> limit (第n页-1)* 每个的个数,每页的页数
-- 每页显示2个,显示第6页的信息,按照年龄从小到大排序
-- 失败 select * from students limit 2*(6-1),2;
-- 失败 select * from students limit 10,2 order by age asc; ----> limit 在最后 优先级 where> order by> limit
-- 连接查询
-- inner join … on
-- 查询 有能够对应班级的学生信息以及班级信息
select * from students inner join classes; --->不能分开
-- 按照要求显示姓名、班级
select students.*,classes.name from students inner join classes on students.cls_id=classes.id;
select students.name,classes.name from students inner join classes on students.cls_id=classes.id;
-- 给数据表起名字
select s.name,c.name from students as s inner join classes as c on s.cls_id=c.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.xxx s.xxx from students as s inner join classes as c on … order by…;
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name;
-- 当时一个班级的时候,按照学生的id 进行从小到大排序
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name, s.id;
-- left join
-- 查询每位学生对应的班级信息
select * from students as s left join classes as c on s.cls_id=c.id; --->以left 左边的标准搜索
-- 查询没有对应班级信息的学生
-- select … from xxx as s left join xxx as c on …. where…
-- select … from xxx as s left join xxx as c on… having…
select * from students as s left join classes as c on s.cls_id=c.id having c.id is null;
select * from students as s left join classes as c on s.cls_id=c.id where c.id is null;
-- right join on
-- 将数据表名字互换位置 ,用left join 完成
-- 自关联
-- 省级联动 url:http://demo.lanrenzhijia.com/2014/city0605/
-- 查询所有省份
-- 查询出山东省有哪些市
select * from areas as provice inner join areas as city on city.pid=province.aid having province.atitle="山东省";
select province.atitle,city.atitle from areas as province inner join areas as ccity on city.pid=province.aid having province.atitle="山东省";
-- 子查询
-- 标量子查询
-- 查询出高于平均身高的信息
-- 查询最高的男生信息
-- select * from students where high=(select max(high) from students);
也可以 select * from area where pid = (select aid from areas where atitle="河北省");
-- 列级子查询
-- 查询学生的班级号能够对应的学生信息
-- select * from students where cls_id in (select id from classes);