时间:2021-07-01 10:21:17 帮助过:2人阅读
-- 向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 or 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 like ‘%靖‘;
例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;
例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 gender=1 and is_delete=0 order by id desc;
例2:查询未删除学生信息,按名称升序
select * from students where is_delete=0 order by name;
例3:显示所有的学生信息,先按照年龄从大-->小排序,当年龄相同时 按照身高从高-->矮排序
select * from students order by age desc,height desc;
为了快速得到统计数据,经常会用到如下5个聚合函数
例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 |
| 保密 | 28.0000 |
+--------+----------+
分别统计性别为男/女的人的个数
select gender,count(*) from students group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 男 | 5 |
| 女 | 7 |
| 中性 | 1 |
| 保密 | 1 |
+--------+----------+
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支持三种类型的连接查询,分别为:
内连接查询:查询的结果为两个表匹配到的数据
右连接查询:查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null填充
左连接查询:查询的结果为两个表匹配到的数据,左表特有的数据,对于右表中不存在的数据使用null填充
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
citys表的proid表示城市所属的省,对应着provinces表的id值
能不能将两个表合成一张表呢?
观察两张表发现,citys表比provinces表多一个列proid,其它列的类型都是一样的
存储的都是地区信息,而且每种信息的数据量有限,没必要增加一个新表,或者将来还要存储区、乡镇信息,都增加新表的开销太大
定义表areas,结构如下
- id
- atitle
- pid
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);
MySQL查询
标签:tween 数据 记录 不同 连接 class values val http