數據庫之多表查詢
阿新 • • 發佈:2018-05-12
distinct -- dep col 基礎 基礎上 sna 右連接 color
一 ,多表查詢
1、內連接:把兩張表有對應關系的記錄連接成一張虛擬表 select * from emp inner join dep on emp.dep_id = dep.id; #應用: select * from emp,dep where emp.dep_id = dep.id and dep.name = "技術"; # 不要用where做連表的活 select * from emp inner join dep on emp.dep_id = dep.id where dep.name = "技術" ; 2、左連接:在內連接的基礎上,保留左邊沒有對應關系的記錄 select* from emp left join dep on emp.dep_id = dep.id; 3、右連接:在內連接的基礎上,保留右邊沒有對應關系的記錄 select * from emp right join dep on emp.dep_id = dep.id; 4、全連接:在內連接的基礎上,保留左、右邊沒有對應關系的記錄 select * from emp left join dep on emp.dep_id = dep.id union select * from emp right join dep on emp.dep_id = dep.id; #補充:多表連接可以不斷地與虛擬表連接查找各部門最高工資 select t1.* from emp as t1 inner join (select post,max(salary) as ms from emp group by post) as t2 on t1.post = t2.post where t1.salary = t2.ms
子查詢:把一個查詢語句用括號括起來,當做另一條查詢語句的條件去用,稱為子查詢 select emp.name from emp inner join dep on emp.dep_id=dep.id where dep.name=‘技術‘ select name fromemp where dep_id= (select id from dep where name=‘技術‘); 查詢平均年齡在25以上的部門 select name from dep where id in (select dep_id from emp group by dep_id having avg(age)>25) select dep.name from emp inner join dep on emp.dep_id=dep.id group by dep.name having avg(age)>25; 查看不足2個人的部門名(子查詢得到的是有人的部門id) select * from emp where exists(select id from dep where id>3); 查詢每一個部門最新入職的那個員工 select t1.id,t1.name,t1.post,t1.hire_date,t2.post,t2.max_date from emp as t1 inner join (select post,max(hire_date) as max_date from emp group by post) as t2 on t1.post=t2.post where t1.hire_date=t2.max_date ;
練習題
1 創建班級表:cid,caption 學生表:sid,sname,gender,class_id 2 插入班級: 1 linux一班 2 linux二班 3 linux三班 4 linux四班 5 python一班 6 python二班 7 python三班 8 python四班 3 插入學生(都在linux2班): (‘王五‘, ‘女‘), (‘王6蛋‘, ‘男‘), (‘王7蛋‘, ‘女‘), (‘王8‘, ‘女‘) ; 4 插入學生:(都在linux3班): (‘劉五‘, ‘女‘), (‘劉6‘, ‘男‘), (‘劉7‘, ‘女‘), (‘劉8‘, ‘女‘) ; 5 插入學生:(都在python1班班): (‘李1‘, ‘女‘, (‘李2‘, ‘男‘), (‘李3‘, ‘女‘), (‘李4‘, ‘女‘) ; 6 插入學生:(都在python2班班): (‘嶽1‘, ‘女‘), (‘嶽2嶽‘, ‘男‘), (‘嶽3嶽‘, ‘女‘), (‘嶽4嶽‘, ‘女‘) ; 7 查詢linux1班學生個數 8 查詢linux2班學生個數 9 查詢linux3班學生個數 10 查詢linux1班 的班級id和所有人的名字,把名字列表的列名命名為names 11 查詢python1班 班級id,學生id最大的人id,所有人的名字,把名字列表的列名命名為names 12 查詢名字裏有1的人的姓名,性別 13 查詢名字叫王(某)某不定:如王4,王2,不包括王2蛋,人的姓名,性別 14 查詢姓王的人的id,性別,姓名,班級id,只取第一條 15 查詢所有學生,按學生id倒排序 16 查詢所有學生,按學生姓名正排序 17 查詢所有學生,按班級倒排序,sid正排序 18 查詢姓王,名字只有倆字的人名和sid,按sid正序排列 19 查詢姓王的人名和sid,按sid正序排列 20 查詢名字以蛋結尾的人名和id,只取id最大的一條 INSERT INTO student (sname, gender, class_id) VALUE (‘王五‘, ‘女‘, 2), (‘王6蛋‘, ‘男‘, 2), (‘王7蛋‘, ‘女‘, 2), (‘王8‘, ‘女‘, 2) ; INSERT INTO student (sname, gender, class_id) VALUE (‘劉五‘, ‘女‘, 3), (‘劉6‘, ‘男‘, 3), (‘劉7‘, ‘女‘, 3), (‘劉8‘, ‘女‘, 3) ; INSERT INTO student (sname, gender, class_id) VALUE (‘李1‘, ‘女‘, 5), (‘李2‘, ‘男‘, 5), (‘李3‘, ‘女‘, 5), (‘李4‘, ‘女‘, 5) ; INSERT INTO student (sname, gender, class_id) VALUE (‘嶽1‘, ‘女‘, 6), (‘嶽2嶽‘, ‘男‘, 6), (‘嶽3嶽‘, ‘女‘, 6), (‘嶽4嶽‘, ‘女‘, 6) ; -- 查詢linux1班學生個數 SELECT count(*) from student GROUP BY class_id HAVING class_id=1; SELECT count(*) from student where class_id=1 select count(*) from student where gender=‘女‘; -- 查詢linux1班 的班級id和所有人的名字,把名字列表的列名命名為names SELECT class_id,GROUP_CONCAT(sname) as names from student GROUP BY class_id HAVING class_id=1; -- 查詢python1班 班級id,學生id最大的人id,所有人的名字,把名字列表的列名命名為names SELECT class_id,max(sid),GROUP_CONCAT(sname) as names from student GROUP BY class_id HAVING class_id=5; -- 查詢名字裏有1的人的姓名,性別 SELECT sname,gender from student where sname like ‘%1%‘ -- 查詢名字叫王(某)某不定:如王4,王2,不包括王2蛋,人的姓名,性別 SELECT sname,gender from student where sname like ‘王_‘ -- 查詢姓王的人的id,性別,姓名,班級id,只取第一條 select * from student where sname like ‘王%‘ LIMIT 1; -- 查詢所有學生,按學生id倒排序 SELECT* from student ORDER BY sid desc; -- 查詢所有學生,按學生姓名正排序 SELECT* from student ORDER BY sname asc; -- 查詢所有學生,按班級倒排序,sid正排序 SELECT * from student ORDER BY class_id desc,sid asc; -- 查詢姓王,名字只有倆字的人名和sid,按sid正序排列 SELECT sname,sid from student where sname like ‘王_‘ ORDER BY sid -- 查詢姓王的人名和sid,按sid正序排列 SELECT sname,sid from student where sname like ‘王%‘ ORDER BY sid -- 查詢名字以蛋結尾的人名和id,只取id最大的一條 SELECT sid,sname from student where sname like ‘%蛋‘ ORDER BY sid desc limit 1;
練習
1、查詢所有的課程的名稱以及對應的任課老師姓名 select course.cname,teacher.tname from course INNER JOIN teacher on course.teacher_id=teacher.tid; 2、查詢學生表中男女生各有多少人 select gender,count(sid) from student group by gender; 3、查詢物理成績等於100的學生的姓名 select student.sname from student WHERE sid IN (select student_id from score INNER JOIN course on score.course_id=course.cid where course.cname=‘物理‘ and score.num=100); 4、查詢平均成績大於八十分的同學的姓名和平均成績 select sname,avg(num) from student inner join score on student.sid = score.student_id group by student_id having avg(num)>80; 5、查詢所有學生的學號,姓名,選課數,總成績 select student.sid,sname,count(course_id),SUM(num) from student INNER JOIN score on student.sid=score.student_id GROUP BY student.sid; 6、 查詢姓李老師的個數 select count(tid) from teacher where tname like ‘李%‘; 7、 查詢沒有報李平老師課的學生姓名 select sname from student where sid not in (select student_id from score where course_id in( select cid from course where teacher_id=(select tid from teacher where tname=‘李平老師‘))); 8、 查詢物理課程比生物課程高的學生的學號 select t1.student_id 學號 from (select student_id ,num from score inner join course on score.course_id=course.cid where cname=‘物理‘ )as t1 inner join (select student_id , num from score inner join course on score.course_id=course.cid where cname = ‘生物‘) as t2 on t1.student_id = t2.student_id where t1.num>t2.num; 9、 查詢沒有同時選修物理課程和體育課程的學生姓名 select sname from student inner join score on student.sid = score.student_id 46 join course on course.cid=score.course_id and cname in (‘物理‘,‘體育‘) 47 group by student_id having count(course_id)!=2; 10、查詢掛科超過兩門(包括兩門)的學生姓名和班級 select sname 姓名,caption 班級 from student inner join score on student.sid = score.course_id join class on class.cid = score.course_id where num<60 group by student_id having count(course_id)>=2; 11 、查詢選修了所有課程的學生姓名 (select student_id,count(course_id) from score group by student_id having count(course_id) = ( select count(cid) from course)) as t1 on t1.student_id = student.sid; 12、查詢李平老師教的課程的所有成績記錄 select num from score inner join course on course.cid=score.course_id join teacher on teacher.tid=course.teacher_id where tname = ‘李平老師‘; 13、查詢全部學生都選修了的課程號和課程名 select cid,cname from course student; 14、查詢每門課程被選修的次數 select course.cname,count(student_id) from score INNER JOIN course on score.course_id=course.cid GROUP BY course_id; 15、查詢之選修了一門課程的學生姓名和學號 select sname 姓名,student_id 學號 from student inner join score on student.sid = score.student_id group by student_id having count(course_id)=1; 16、查詢所有學生考出的成績並按從高到低排序(成績去重) select DISTINCT num from score ORDER BY num desc; 17、查詢平均成績大於85的學生姓名和平均成績 select sname 姓名,avg(num) 平均成績 from student inner join score on student.sid = score.student_id group by student_id having avg(num)>85; 18、查詢生物成績不及格的學生姓名和對應生物分數 select student.sname ,num 生物成績 from student inner join score on student.sid = score.student_id join course on course.cid=score.course_id where cname=‘生物‘ and num<60; 19、查詢在所有選修了李平老師課程的學生中,這些課程(李平老師的課程,不是所有課程)平均成績最高的學生姓名 select sname from student where sid=( select student_id from score where course_id in ( select cid from course where teacher_id=(select tid from teacher where tname=‘李平老師‘) ) group by student_id order by avg(num) desc limit 1 ) 20、查詢每門課程成績最好的前兩名學生姓名
文件
1、查詢所有的課程的名稱以及對應的任課老師姓名 select course.cname,teacher.tname from course INNER JOIN teacher on course.teacher_id=teacher.tid; 2、查詢學生表中男女生各有多少人 select gender,count(sid) from student group by gender; 3、查詢物理成績等於100的學生的姓名 select student.sname from student WHERE sid IN (select student_id from score INNER JOIN course on score.course_id=course.cid where course.cname=‘物理‘ and score.num=100); 4、查詢平均成績大於八十分的同學的姓名和平均成績 select sname,avg(num) from student inner join score on student.sid = score.student_id group by student_id having avg(num)>80; 5、查詢所有學生的學號,姓名,選課數,總成績 select student.sid,sname,count(course_id),SUM(num) from student INNER JOIN score on student.sid=score.student_id GROUP BY student.sid; 6、 查詢姓李老師的個數 select count(tid) from teacher where tname like ‘李%‘; 7、 查詢沒有報李平老師課的學生姓名 select sname from student where sid not in (select student_id from score where course_id in( select cid from course where teacher_id=(select tid from teacher where tname=‘李平老師‘))); 8、 查詢物理課程比生物課程高的學生的學號 select t1.student_id 學號 from (select student_id ,num from score inner join course on score.course_id=course.cid where cname=‘物理‘ )as t1 inner join (select student_id , num from score inner join course on score.course_id=course.cid where cname = ‘生物‘) as t2 on t1.student_id = t2.student_id where t1.num>t2.num; 9、 查詢沒有同時選修物理課程和體育課程的學生姓名 select sname from student inner join score on student.sid = score.student_id 46 join course on course.cid=score.course_id and cname in (‘物理‘,‘體育‘) 47 group by student_id having count(course_id)!=2; 10、查詢掛科超過兩門(包括兩門)的學生姓名和班級 select sname 姓名,caption 班級 from student inner join score on student.sid = score.course_id join class on class.cid = score.course_id where num<60 group by student_id having count(course_id)>=2; 11 、查詢選修了所有課程的學生姓名 (select student_id,count(course_id) from score group by student_id having count(course_id) = ( select count(cid) from course)) as t1 on t1.student_id = student.sid; 12、查詢李平老師教的課程的所有成績記錄 select num from score inner join course on course.cid=score.course_id join teacher on teacher.tid=course.teacher_id where tname = ‘李平老師‘; 13、查詢全部學生都選修了的課程號和課程名 select cid,cname from course student; 14、查詢每門課程被選修的次數 select course.cname,count(student_id) from score INNER JOIN course on score.course_id=course.cid GROUP BY course_id; 15、查詢之選修了一門課程的學生姓名和學號 select sname 姓名,student_id 學號 from student inner join score on student.sid = score.student_id group by student_id having count(course_id)=1; 16、查詢所有學生考出的成績並按從高到低排序(成績去重) select DISTINCT num from score ORDER BY num desc; 17、查詢平均成績大於85的學生姓名和平均成績 select sname 姓名,avg(num) 平均成績 from student inner join score on student.sid = score.student_id group by student_id having avg(num)>85; 18、查詢生物成績不及格的學生姓名和對應生物分數 select student.sname ,num 生物成績 from student inner join score on student.sid = score.student_id join course on course.cid=score.course_id where cname=‘生物‘ and num<60; 19、查詢在所有選修了李平老師課程的學生中,這些課程(李平老師的課程,不是所有課程)平均成績最高的學生姓名 select sname from student where sid=( select student_id from score where course_id in ( select cid from course where teacher_id=(select tid from teacher where tname=‘李平老師‘) ) group by student_id order by avg(num) desc limit 1 ) 20、查詢每門課程成績最好的前兩名學生姓名
數據庫之多表查詢