提供以下自己手里的资源供大家做做练习,也方便自己做做回顾
创建数据库
create database if not exists `ecid`;
创建案件表ajxx 并插入测试数据
use ecid;
DROP TABLE IF EXISTS `ajxx`;
CREATE TABLE `ajxx` (
`ajbh` varchar(50) NOT NULL COMMENT '案件编号',
`dept_id` varchar(50) NOT NULL COMMENT '部门号',
`zhrs` int(5) DEFAULT NULL COMMENT '抓获人数'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `ajxx` VALUES ('100000', '1011', '1');
INSERT INTO `ajxx` VALUES ('100001', '1002', '5');
INSERT INTO `ajxx` VALUES ('100003', '1003', '5');
INSERT INTO `ajxx` VALUES ('11111', '1000', '2');
INSERT INTO `ajxx` VALUES ('22222', '1001', '3');
INSERT INTO `ajxx` VALUES ('33333', '1006', '4');
INSERT INTO `ajxx` VALUES ('44444', '1002', '4');
INSERT INTO `ajxx` VALUES ('55555', '1003', '5');
INSERT INTO `ajxx` VALUES ('66666', '1005', '4');
INSERT INTO `ajxx` VALUES ('77777', '1007', '2');
INSERT INTO `ajxx` VALUES ('88888', '1008', '8');
INSERT INTO `ajxx` VALUES ('99999', '1010', '2');
INSERT INTO `ajxx` VALUES ('100001', '1003', '6');
INSERT INTO `ajxx` VALUES ('44444', '1003', '5');
创建部门表dept并插入测试数据
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
`deptid` varchar(50) NOT NULL COMMENT '部门编号',
`name` varchar(50) NOT NULL COMMENT '部门名称',
PRIMARY KEY (`deptid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `dept` VALUES ('1000', 'aa');
INSERT INTO `dept` VALUES ('1001', 'bb');
INSERT INTO `dept` VALUES ('1002', '哈尔滨');
INSERT INTO `dept` VALUES ('1003', '大庆');
INSERT INTO `dept` VALUES ('1004', 'ee');
INSERT INTO `dept` VALUES ('1005', 'ff');
INSERT INTO `dept` VALUES ('1006', 'gg');
INSERT INTO `dept` VALUES ('1007', 'hh');
INSERT INTO `dept` VALUES ('1008', 'ii');
INSERT INTO `dept` VALUES ('1009', 'jj');
INSERT INTO `dept` VALUES ('1010', 'kk');
INSERT INTO `dept` VALUES ('1011', 'll');
INSERT INTO `dept` VALUES ('1012', 'mm');
INSERT INTO `dept` VALUES ('1013', 'nn');
INSERT INTO `dept` VALUES ('1014', 'oo');
INSERT INTO `dept` VALUES ('1015', 'pp');
创建民警信息表 st_user并插入测试数据
DROP TABLE IF EXISTS `st_user`;
CREATE TABLE `st_user` (
`name` varchar(50) NOT NULL COMMENT '民警姓名',
`dept_id` varchar(50) NOT NULL COMMENT '部门号',
`userid` varchar(10) NOT NULL COMMENT '警号',
`phone` int(11) NOT NULL COMMENT '手机号',
PRIMARY KEY (`name`),
UNIQUE KEY `user_id_unique` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `st_user` VALUES ('不不不', '1002', '002', '12345465');
INSERT INTO `st_user` VALUES ('买买买', '1002', '013', '785466');
INSERT INTO `st_user` VALUES ('去去去', '1003', '015', '8544646');
INSERT INTO `st_user` VALUES ('呃呃呃', '1005', '005', '8496894');
INSERT INTO `st_user` VALUES ('呸呸呸', '1008', '014', '4645498');
INSERT INTO `st_user` VALUES ('哈哈哈', '1008', '008', '4846168');
INSERT INTO `st_user` VALUES ('唉唉唉', '1009', '009', '8464894');
INSERT INTO `st_user` VALUES ('啊啊啊', '1001', '001', '12345648');
INSERT INTO `st_user` VALUES ('啦啦啦', '1001', '012', '485646');
INSERT INTO `st_user` VALUES ('嘎嘎嘎', '1007', '007', '6546846');
INSERT INTO `st_user` VALUES ('急急急', '1010', '010', '48944654');
INSERT INTO `st_user` VALUES ('烦烦烦', '1006', '006', '89748964');
INSERT INTO `st_user` VALUES ('热热热', '1003', '016', '9856484');
INSERT INTO `st_user` VALUES ('踩踩踩', '1003', '003', '4654616');
INSERT INTO `st_user` VALUES ('酷酷酷', '1011', '011', '8456464');
INSERT INTO `st_user` VALUES ('顶顶顶', '1004', '004', '4846546');
创建学生-课程-分数表
CREATE TABLE `student`(
`name` varchar(50) DEFAULT NULL,
`sub` varchar(50) DEFAULT NULL,
`score` int(11) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8mb4
insert student (name,sub,score) values ('李四','数学',89);
insert student (name,sub,score) values ('李四','语文',93);
insert student (name,sub,score) values ('李四','英语',91);
insert student (name,sub,score) values ('王文','数学',63);
insert student (name,sub,score) values ('王文','语文',90);
insert student (name,sub,score) values ('王文','英语',90);
insert student (name,sub,score) values ('张娜','数学',90);
insert student (name,sub,score) values ('张娜','语文',88);
insert student (name,sub,score) values ('张娜','英语',90);
insert student (name,sub,score) values ('王菲','数学',90);
insert student (name,sub,score) values ('王菲','语文',93);
insert student (name,sub,score) values ('王菲','英语',70);
insert student (name,sub,score) values ('周一','数学',90);
insert student (name,sub,score) values ('周一','语文',96);
insert student (name,sub,score) values ('周一','英语',90);
insert student (name,sub,score) values ('陈二','数学',92);
insert student (name,sub,score) values ('陈二','语文',93);
insert student (name,sub,score) values ('陈二','英语',92);
insert student (name,sub,score) values ('陈二','科学',95);
insert student (name,sub,score) values ('张三','数学',92);
insert student (name,sub,score) values ('张三','语文',93);
insert student (name,sub,score) values ('张三','英语',92);
insert student (name,sub,score) values ('张三','科学',95);
insert student (name,sub,score) values ('张三','生物',91);
(1)查询抓获人数前两名的部门 显示抓获人数和部门号
(2)查询统计出哪些部门办理的案件数量大于1起,显示部门名称和案件总数
(3)查询出抓获人数最多的案件所对应的部门下,有多少位民警,要求展出name列和部门号列
(4)查询抓获人数最多的部门下,有多少民警,要求展示 name 和 部门号列
(5)查询出同一案件编号情况下大庆市抓获人数比哈尔滨市抓获人数多的案件,输出案件编号
(哈尔滨市部门编号1002,大庆1003)
(6)请统计出,一共有多少个部门下面没有民警,输出结果值
(7)在大于全省平均抓获人数的部门中查询出,排名前2位的部门名称,输出部门名称
(8)找到student表中所有课程都超过90分的学生
(9)找到student表中平均分排在第4~第6名的学生
(1)select zhrs as 抓获人数,dept_id as 部门号 from ajxx order by zhrs DESC limit 0,2;
(2)select name ,ajs 案件总数
from (SELECT dept_id ,COUNT(ajbh) as ajs from ajxx GROUP BY dept_id) a
INNER JOIN dept b
on a.dept_id=b.deptid WHERE ajs>1;
(3)select name,dept_id AS 部门号 from st_user where dept_id =(select dept_id FROM ajxx ORDER BY zhrs DESC limit 0,1);
或者:select deptid,b.name from dept a,st_user b where a.deptid=b.dept_id and a.deptid=(select dept_id from ajxx order by zhrs desc limit 0,1);
(4)select name ,a.dept_id as 部门号
from st_user a
INNER JOIN (select dept_id,SUM(zhrs) as amount from ajxx GROUP BY dept_id ORDER BY amount desc limit 0,1) b
on a.dept_id = b.dept_id;
(5)select a.ajbh from
(select ajbh,zhrs from ajxx where dept_id ='1002') a INNER JOIN
(select ajbh,zhrs from ajxx where dept_id ='1003') b
ON a.ajbh = b.ajbh and a.zhrs < b.zhrs;
(6)select COUNT(deptid) 没有民警的部门数 from dept where deptid not in (select DISTINCT dept_id from st_user);
(7)select a.name
FROM dept a,
(select dept_id,SUM(zhrs) as amount from ajxx GROUP BY dept_id) b ,
(SELECT AVG(all amount) as avg1 from (select dept_id,SUM(zhrs) as amount from ajxx GROUP BY dept_id) d) c
WHERE a.deptid = b.dept_id AND b.amount > c.avg1
order by amount DESC
limit 0,2;
(8)#方式一:利用case when then else end(when 1 then '男' else '女' end)
select name
from student group by name
having count(score) = sum(case when score>=90 then 1 else 0 end);
#方法二:所有超过90分的不好整,就找存在低于90分的
select name
from student group by name
having name not in (select name from student where score <90);
#方式三:所有超过90分的不好整,就找分组后最低分超过90分的
select name
from student group by name
having min(score) >= 90;
#补充having: having不能简单理解为何group by固定搭配,并不是固定的。
#having和where的区别在于,where判断的字段要是数据库中有的而且是前面sql语句没有进行筛选过的,而having不需要having后面的条件字段是就是前面筛选过的。
#比如方法一中 count(score)是数据库中没有的字段,方法二中name是前面经历过group by 筛选过的,上面将having换成where都会报错。
(9)select name,sum(score)/count(score) as avg
from student group by name
order by avg desc limit 4,2;