时间:2021-07-01 10:21:17 帮助过:40人阅读
数据准备
drop table if exists class;
create table class(
class_no int(2) unsigned zerofill primary key auto_increment comment ‘班级编号‘,
class_name varchar(30) not null comment ‘班级名称‘
);
insert into class values(1, ‘培优班‘);
insert into class values(2, ‘普通班‘);
drop table if exists student;
create table student(
stu_no int(2) unsigned zerofill primary key auto_increment comment ‘学员编号‘,
stu_name varchar(30) not null comment ‘学员姓名‘,
stu_sex varchar(3) not null comment ‘学员性别‘,
stu_age tinyint(2) unsigned zerofill comment ‘学员年代‘,
class_no int(2) unsigned zerofill comment ‘所在班级编号‘,
foreign key(class_no) references class(class_no)
);
insert into student values(01, ‘李白‘, ‘男‘, 18, 01);
insert into student values(02, ‘杜甫‘, ‘男‘, 20, 01);
insert into student values(03, ‘张飞‘, ‘男‘, 32, 02);
insert into student values(04, ‘韩信‘, ‘男‘, 26, 02);
insert into student values(05, ‘了龙‘, ‘男‘, 27, 02);
insert into student values(06, ‘大乔‘, ‘女‘, 17, 01);
insert into student values(07, ‘小乔‘, ‘女‘, 16, 01);
insert into student values(08, ‘小乔‘, ‘女‘, 16, 01);
insert into student values(09, ‘关哥‘, ‘男‘, 32, 02);
insert into student values(10, ‘刘备‘, ‘男‘, 36, null);
alter table student drop foreign key `student_ibfk_1`;
1: 查询出student表中年龄最大的学生
select * from student where stu_age = (select max(stu_age) from student);
2: 查询出student表中年龄最小的学生
select * from student where stu_age = (select min(stu_age) from student);
3: 查询出02号班中最大的年龄是多少
select max(stu_age) from student where class_no = 2;
4: 查询出01号班中最小的年龄是多少
select min(stu_age) from student where class_no = 1;
5: 查询出01号班中有多少个学生
select count(*) from student where class_no = 1;
6: 查询出01号班中平均年龄是多少
select avg(stu_age) from student where class_no = 1;
7:查询出没有班级的学生
(失败)select * from student where class_no not in ( select class_no from class);(查询出结果为空?)
(成功)select * from student where not exists ( select class_no from class where student.class_no = class.class_no); // null值的特殊性,不能使用not in来查询,NULL值在与任意值比较时总是假的(FALSE),并且包含NULL的一个表达式总是产生一个NULL值
8: 查询出02号班级中年龄大于30岁的男学员
select * from student where class_no = 2 and stu_sex = ‘男‘ and stu_age > 30;
9: 查询出所有的女学员姓名,并在名字后加上‘大美女’名字
select concat(stu_name,‘大美女‘) from student where stu_sex = ‘女‘;
10: 查询出1号班级的所有男学员 还有 没有班级的学员
select * from student where (class_no = 1 || class_no is null) and stu_sex = ‘男‘;
select * from student where class_no = 1 and stu_sex = ‘男‘ || not exists ( select class_no from class where student.class_no = class.class_no);
11: 查询出年龄在20-30岁的学员
select * from student where stu_age between 20 and 30;
12: 查询出所有名字中第二个字符是‘乔’的学员的平均工资(没有工资列,改为平均年龄或者添加工资列)
select avg(stu_age) from student where stu_name like(‘_乔%‘);
13: 查询出班中所有学生的姓名,其中不包括重复的
select distinct(stu_name) from student where class_no is not null;
14: 查询姓名,性别两个字段, 不包括重复的记录
select distinct stu_name,stu_age from student;
15: 查询出1号部门中所有的学员,根据年龄升序排序,年龄相同的则按照学号降序排序
select * from student where class_no = 1 order by stu_age asc,stu_no desc;
16: 查询出最后一条数据
(失败)select * from student limit (select count(stu_no)-1 from student),1;// 参数不能为表达式
select * from student order by stu_no desc limit 1;
17: 查询出学号为6的后面的3条数据
select * from student where stu_no > 6 limit 3;
18: 查询出学号为6的前面的3条数据
select * from student where stu_no < 6 order by stu_no desc limit 3;
19: 删除最后一条记录,(在不知道最后一条记录id的情况)
delete from student where stu_no in (select stu_no from (select stu_no from student order by stu_no desc limit 1) as temtable);// MySQL不能指定更新的目标表在FROM子句,所以先将删除的数据放到临时表中再进行删除
20: 删除掉学号为6的后面的3条数据(同理(19))
delete from student where stu_no in (select stu_no from (select stu_no from student where stu_no > 6 limit 3) as temtable);
Mysql查询小作业
标签:rem comment exists table enc class max exist esc