Mysql查詢小作業
數據準備
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查詢小作業