1. 程式人生 > >mysql之查詢

mysql之查詢

運算符 元素 等於 var and != 李白 運算 準備


#數據準備
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 ‘學員年代‘,
grade double(5,2) zerofill comment ‘成績‘,
class_no int(2) unsigned zerofill comment ‘所在班級編號‘,
foreign key(class_no) references class(class_no)
);
insert into student values(01, ‘李白‘, ‘男‘, 18, 60, 01);
insert into student values(02, ‘杜甫‘, ‘男‘, 20, 76, 01);
insert into student values(03, ‘張飛‘, ‘男‘, 32, 80, 02);
insert into student values(04, ‘韓信‘, ‘男‘, 26, 98, 02);
insert into student values(05, ‘了龍‘, ‘男‘, 27, 56, 02);
insert into student values(06, ‘大喬‘, ‘女‘, 17, 88, 01);
insert into student values(07, ‘小喬‘, ‘女‘, 16, 96, 01);
insert into student values(08, ‘小喬‘, ‘女‘, 16, 90, 01);
insert into student values(09, ‘關哥‘, ‘男‘, 32, 80, 02);
insert into student values(10, ‘劉備‘, ‘男‘, 36, 98, null);
alter table student drop foreign key `student_ibfk_1`;
*******************************************************************************************************************************

1: [ group by ] 分組
需求: 查詢出每一個班級最高成績是多少
例: select class_no, max(grade) from student group by class_no; --先按class_no分組,然後再拿到每一組的最高成績
例: select class_no, sum(grade) from student group by class_no; --查詢每一個班級的總成績
例: select class_no, min(grade) from student group by class_no; --查詢每一個班級的最低成績
例: select class_no, avg(grade) from student group by class_no; --查詢每一個班級的平均成績
例: select class_no, count(*) from student group by class_no; --查詢每一個班級的人數
ps: group by一般要與max,min,avg等這些函數一起使用
例: select * from student group by class_no;

--可以進多重分組
需求: 查詢出每一個班級男學生和女學生的最高成績分別是多少
例: select class_no, stu_sex ,max(grade) from student group by class_no, stu_sex;

例: select class_no, stu_name, max(grade) from student group by class_no; --不能這樣做
ps: 在要查詢的字段集中,這些字段要麽是包含在group by語句的後面的字段, 要麽就是被包含在聚合函數中, 否則會報錯


--還可以對滿足條件的記錄進行分組
select class_no, max(grade) from student where class_no is not null group by class_no;
*******************************************************************************************************************************

2: having
例: select * from student where class_no = 1 and stu_sex = ‘男‘;
例: select * from student having class_no = 1 and stu_sex = ‘男‘;

例: select class_no, max(grade) from student group by class_no having class_no is not null; --having
例: select class_no, max(grade) from student group by class_no where class_no is not null; --報錯
ps: having需要跟在group by後面,而where不能跟在group by後面
*******************************************************************************************************************************

查詢它可以配合5個字句來執行,查詢到相應數據(where, order by ,limit, group by, having)
這個子句有一個順序,需要按照順序來寫
select * from student [where] [group by] [having] [order by] [limit];
例: select class_no, stu_sex, avg(grade) from student where(class_no is not null) group by class_no,stu_sex having(stu_sex = ‘男‘) order by class_no desc limit 1;
*******************************************************************************************************************************

3: [ in ](集合運算符)
需求: 查詢出學號是2,3,5的學生
例: select * from student where stu_no = 2 or stu_no = 3 or stu_no =5; --使用or運算符
例: select * from student where stu_no in (2,3,5); --使用in集合運算符

例: select * from student where class_no in (2, null); --in集合運算符查詢不到null值

--還可以有not in
例: select * from student where stu_no not in (2,3,5);
例: select * from student where class_no not in (2, null); --查詢結果為空
*******************************************************************************************************************************

4: 子查詢
需求: 獲取student表裏成績成績最高的學員
例: select * from student order by grade desc limit 1; --這裏不滿足需求
例: select * from student where grade = max(grade); --報錯

例: select * from student where grade = (select max(grade) from student);
ps: 在查詢裏邊還有其它的查詢,那麽我們就把裏邊的查詢叫做子查詢,子查詢需要括號包起來, 子查詢可以有多個


ps: 子查詢其實也就是一個查詢,所以它返回的結果有以下幾種情況(單一值),(一列),(一行或者一行多列),(多行多列)
根據返回值的情況不同,可以把子查詢分為四種情況
1: 單一值(標量子查詢)
需求: 獲取student表裏成績成績最高的學員
例: select * from student where grade = (select max(grade) from student);

2: 一列(列子查詢)
例: select stu_age from student where grade < 95;

需求: 查詢出成績小於95的學員年齡
[ in ](集合運算符)
例: select * from student where stu_age in (select stu_age from student where grade < 95); --in在集合中存在的
例: select * from student where stu_age not in (select stu_age from student where grade < 95); --not in在集合中不存在的
[ any ]
例: select * from student where stu_age = any(select stu_age from student where grade < 95); -- =any等於集合中任意一個就行
例: select * from student where stu_age != any(select stu_age from student where grade < 95); -- !=any不滿足集合中任意一個就行
[ all ]
例: select * from student where stu_age = all(select stu_age from student where grade < 95); -- =all等於集合中所有元素
例: select * from student where stu_age != all(select stu_age from student where grade < 95); -- !=不等於集合中的所有元素(意思就是集合中不存在的)

3: 一行(行子查詢)
需求: 查詢出同一班中相同成績的 學生的姓名,班級,成績;
select stu_name, class_no, grade from student where (class_no, grade) = (select class_no,grade from student group by class_no, grade having count(*) > 1);
ps: (class_no, grade)意思是臨時構造成一個行,根據子查詢到一行去比較;


4: 多行多列(表子查詢)
需求: 查詢出表中的stu_name,stu_sex的字段,要使用子查詢
例: select * from (select stu_name, stu_sex from student) as stu;
ps:from後面需要跟一個表,如果是跟著是一個子查詢得的一個臨時表,那麽你需要給這個字查詢起一個加名;

mysql之查詢