-- 创建数据库
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 表名;
例:
select * from students;
select 列1,列2,... from 表名;
例:
select name from students;
select id as 序号, name as 名字, gender as 性别 from students;
-- 如果是单表查询 可以省略表明
select id, name, gender from students;
-- 表名.字段名
select students.id,students.name,students.gender from students;
-- 可以通过 as 给表起别名
select s.id,s.name,s.gender from students as s;
select distinct 列1,... from 表名;
例:
select distinct gender from students;
使用where子句对表中的数据筛选,结果为true的行会出现在结果集中
select * from 表名 where 条件;
例:
select * from students where id=1;
=
>
>=
<
<
!=
或 <>
例1:查询编号大于3的学生
select * from students where id > 3;
例2:查询编号不大于4的学生
select * from students where id <= 4;
例3:查询姓名不是"黄蓉"的学生
select * from students where name <= "黄蓉";
例4:查询没有被删除的学生
select * from students where is_delete=0;
例5:查询编号大于3的女同学
select * from students where id > 3 and gender=0;
例6:查询编号小于4或没被删除的学生
select * from students where id < 4 and is_delete=0;
%
表示任意多个字符_
表示一个字符例7:查询姓黄的学生
select * from students where name like "黄%";
例8:查询姓黄并且名字是一个字的学生
select * from students where name like "黄_";
例9:查询姓黄或叫靖的学生
select * from students where name like "黄%" or name="%靖";
in
表示在一个非连续的范围内例10:查询编号是1或者3或者8的同学
select * from students where id in (1,3,8);
例11:查询编号为3至8的学生
select * from students where id between 3 and 8;
例12:查询编号是3至8的男生
select * from students where (id between 3 and 8) and gender=1;
null
与 ""
是不同的isnull
isnotnull
例13:查询没有填写身高的学生
select * from students where height is null;
例14:查询填写了身高的学生
select * from students where height is not null;
例15:查询填写了身高的男生
select * from students where height is not null and gender=1;
select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]
例1:查询未删除男生信息,按照学号降序
select * from students where is_delete=0 ordaer by id desc;
例2:查询未删除学生信息,按名称升序
select * from students where is_delete=0 order by name;
例3:显示所有的学生信息,先按照年龄从大-->小排序,当年龄相同时按照身高从高-->矮排序
select * from students order by age desc,height desc;
count(*)
表示计算总行数,括号中写星与列名,结果是相同的例1:查询学生总数
select count(*) from students;
例2:查询女生的编号最大值
select max(id) from students where gender=2;
例3:查询未删除的学生最小编号
select min(id) from students where is_delete=0;
例4:查询男生的总年龄
select sum(age) from students where gender=1;
-- 平均年龄
select sum(age)/count(*) from students where gender=1;
例5:查询未删除女生的编号平均值
select avg(id) from students where is_delete=0 and gender=2;
select * from students;
+----+-----------+------+--------+--------+--------+-----------+
| id | name | age | height | 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 | |
| 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | |
| 8 | 周杰伦 | 36 | NULL | 男 | 1 | |
| 9 | 程坤 | 27 | 181.00 | 男 | 2 | |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | |
| 11 | 金星 | 33 | 162.00 | 中性 | 3 | |
| 12 | 静香 | 12 | 180.00 | 女 | 4 | |
| 13 | 周杰 | 34 | 176.00 | 女 | 5 | |
| 14 | 郭靖 | 12 | 170.00 | 男 | 4 | |
+----+-----------+------+--------+--------+--------+-----------+
select gender from students group by gender;
+--------+
| gender |
+--------+
| 男 |
| 女 |
| 中性 |
| 保密 |
+--------+
根据gender字段来分组,gender字段的全部值有4个'男','女','中性','保密',所以分为了4组当group by单独使用时,只显示出每组的第一条记录, 所以group by单独使用时的实际意义不大
select gender from students group by gender;
+--------+
| gender |
+--------+
| 男 |
| 女 |
| 中性 |
| 保密 |
+--------+
select gender,group_concat(name) from students group by gender;
+--------+-----------------------------------------------------------+
| gender | group_concat(name) |
+--------+-----------------------------------------------------------+
| 男 | 彭于晏,刘德华,周杰伦,程坤,郭靖 |
| 女 | 小明,小月月,黄蓉,王祖贤,刘亦菲,静香,周杰 |
| 中性 | 金星 |
| 保密 | 凤姐 |
+--------+-----------------------------------------------------------+
select gender,group_concat(id) from students group by gender;
+--------+------------------+
| gender | group_concat(id) |
+--------+------------------+
| 男 | 3,4,8,9,14 |
| 女 | 1,2,5,7,10,12,13 |
| 中性 | 11 |
| 保密 | 6 |
+--------+------------------+
值的集合
做一些操作select gender,group_concat(age) from students group by gender;
+--------+----------------------+
| gender | group_concat(age) |
+--------+----------------------+
| 男 | 29,59,36,27,12 |
| 女 | 18,18,38,18,25,12,34 |
| 中性 | 33 |
| 保密 | 28 |
+--------+----------------------+
分别统计性别为男/女的人年龄平均值
select gender,avg(age) from students group by gender;
+--------+----------+
| gender | avg(age) |
+--------+----------+
| 男 | 32.6000 |
| 女 | 23.2857 |
| 中性 | 33.0000 |
select gender,count(*) from students group by gender having count(*)>2;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 男 | 5 |
| 女 | 7 |
+--------+----------+
select gender,count(*) from students group by gender with rollup;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 男 | 5 |
| 女 | 7 |
| 中性 | 1 |
| 保密 | 1 |
| NULL | 14 |
+--------+----------+
select gender,group_concat(age) from students group by gender with rollup;
+--------+-------------------------------------------+
| gender | group_concat(age) |
+--------+-------------------------------------------+
| 男 | 29,59,36,27,12 |
| 女 | 18,18,38,18,25,12,34 |
| 中性 | 33 |
| 保密 | 28 |
| NULL | 29,59,36,27,12,18,18,38,18,25,12,34,33,28 |
+--------+-------------------------------------------+
select * from 表名 limit start,count
例1:查询前3行男生信息
select * from students where gender=1 limit 0,3;
select * from students where is_delete=0 limit (n-1)*m,m
当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回
mysql支持三种类型的连接查询,分别为:
select * from 表1 inner或left或right join 表2 on 表1.列 = 表2.列
例1:使用内连接查询班级表与学生表
select * from students inner join classes on students.cls_id = classes.id;
例2:使用左连接查询班级表与学生表
select * from students as s left join classes as c on s.cls_id = c.id;
例3:使用右连接查询班级表与学生表
select * from students as s right join classes as c on s.cls_id = c.id;
例4:查询学生姓名及班级名称
select s.name,c.name from students as s inner join classes as c on s.cls_id = c.id;
能不能将两个表合成一张表呢?
观察两张表发现,citys表比provinces表多一个列proid,其它列的类型都是一样的
存储的都是地区信息,而且每种信息的数据量有限,没必要增加一个新表,或者将来还要存储区、乡镇信息,都增加新表的开销太大
定义表areas,结构如下
create table areas(
aid int primary key,
atitle varchar(20),
pid int
);
source areas.sql;
select count(*) from areas where pid is null;
select city.* from areas as city
inner join areas as province on city.pid=province.aid
where province.atitle='山西省';
select dis.* from areas as dis
inner join areas as city on city.aid=dis.pid
where city.atitle='广州市';
在一个select 语句中,嵌入了另外一个select 语句, 那么被嵌入的select 语句称之为子查询语句
主要查询的对象,第一条select 语句
查询班级学生的平均身高
select * from students where age > (select avg(age) from students);
select name from classes where id in (select cls_id from students);
select * from students where (height,age) = (select max(height),max(age) from students);
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
本文分享自 Tyrant Lucifer 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!