多表查询

多表查询,第1张

概述一多表连接查询#重点:外链接语法SELECT字段列表 一 多表连接查询<span >SELECT<span > 字段列表
<span >FROM 表1 <span >INNER<span >|left|RIGHT <span >JOIN<span > 表2
<span >ON 表1.字段 <span >= 表2.字段;

(<span >create <span >table<span > employee(
ID
<span >int
<span >primary
<span >key
<span > auto_increment,name
<span >varchar
(<span >20<span >),sex enum(<span >'<span >male<span >',<span >'<span >female<span >') <span >not <span >null <span >default <span >'<span >male<span >'<span >,age <span >int<span >,dep_ID <span >int<span >
);

插入数据

<span >insert <span >into department <span >values<span >
(<span >200,<span >'<span >技术<span >'<span >),(<span >201,<span >'<span >人力资源<span >'<span >),(<span >202,<span >'<span >销售<span >'<span >),(<span >203,<span >'<span >运营<span >'<span >);

<span >insert <span >into employee(name,sex,age,dep_ID) <span >values<span >
(<span >'<span >egon<span >',<span >'<span >male<span >',<span >18,<span >200<span >),(<span >'<span >alex<span >',<span >'<span >female<span >',<span >48,<span >201<span >),(<span >'<span >wupeiqi<span >',<span >38,(<span >'<span >yuanhao<span >',<span >28,<span >202<span >),(<span >'<span >liwenzhou<span >',(<span >'<span >jingliyang<span >',<span >204<span >)
;

查看表结构和数据

MysqL<span >> <span >desc<span > department;
<span >+<span >--<span >-----+-------------+------+-----+---------+-------+
<span >| FIEld <span >| Type <span >| <span >Null <span >| <span >Key <span >| <span >Default <span >| Extra <span >|
<span >+<span >--<span >-----+-------------+------+-----+---------+-------+
<span >| ID <span >| <span >int(<span >11) <span >| YES <span >| <span >| <span >NulL <span >| <span >|
<span >| name <span >| <span >varchar(<span >20) <span >| YES <span >| <span >| <span >NulL <span >| <span >|
<span >+<span >--<span >-----+-------------+------+-----+---------+-------+
<span >
MysqL<span >> <span >desc<span > employee;
<span >+<span >--<span >------+-----------------------+------+-----+---------+----------------+
<span >| FIEld <span >| Type <span >| <span >Null <span >| <span >Key <span >| <span >Default <span >| Extra <span >|
<span >+<span >--<span >------+-----------------------+------+-----+---------+----------------+
<span >| ID <span >| <span >int(<span >11) <span >| NO <span >| PRI <span >| <span >NulL <span >| auto_increment <span >|
<span >| name <span >| <span >varchar(<span >20) <span >| YES <span >| <span >| <span >NulL <span >| <span >|
<span >| sex <span >| enum(<span >'<span >male<span >',<span >'<span >female<span >') <span >| NO <span >| <span >| male <span >| <span >|
<span >| age <span >| <span >int(<span >11) <span >| YES <span >| <span >| <span >NulL <span >| <span >|
<span >| dep_ID <span >| <span >int(<span >11) <span >| YES <span >| <span >| <span >NulL <span >| <span >|
<span >+<span >--<span >------+-----------------------+------+-----+---------+----------------+
<span >
MysqL<span >> <span >select <span > <span >from<span > department;
<span >+<span >--<span >----+--------------+
<span >| ID <span >| name <span >|
<span >+<span >--<span >----+--------------+
<span >| <span >200 <span >| 技术 <span >|
<span >| <span >201 <span >| 人力资源 <span >|
<span >| <span >202 <span >| 销售 <span >|
<span >| <span >203 <span >| 运营 <span >|
<span >+<span >--<span >----+--------------+
<span >
MysqL<span >> <span >select <span >
<span >from<span > employee;
<span >+<span >--<span >--+------------+--------+------+--------+
<span >| ID <span >| name <span >| sex <span >| age <span >| dep_ID <span >|
<span >+<span >--<span >--+------------+--------+------+--------+
<span >| <span >1 <span >| egon <span >| male <span >| <span >18 <span >| <span >200 <span >|
<span >| <span >2 <span >| alex <span >| female <span >| <span >48 <span >| <span >201 <span >|
<span >| <span >3 <span >| wupeiqi <span >| male <span >| <span >38 <span >| <span >201 <span >|
<span >| <span >4 <span >| yuanhao <span >| female <span >| <span >28 <span >| <span >202 <span >|
<span >| <span >5 <span >| liwenzhou <span >| male <span >| <span >18 <span >| <span >200 <span >|
<span >| <span >6 <span >| jingliyang <span >| female <span >| <span >18 <span >| <span >204 <span >|
<span >+<span >--<span >--+------------+--------+------+--------+

1 交叉连接:不适用任何匹配条件。生成笛卡尔积

MysqL ID name sex age dep_ID ID name egon male 技术 egon male 人力资源 egon male 销售 egon male 运营 alex female 技术 alex female 人力资源 alex female 销售 alex female 运营 wupeiqi male 技术 wupeiqi male 人力资源 wupeiqi male 销售 wupeiqi male 运营 yuanhao female 技术 yuanhao female 人力资源 yuanhao female 销售 yuanhao female 运营 liwenzhou male 技术 liwenzhou male 人力资源 liwenzhou male 销售 liwenzhou male 运营 jingliyang female 技术 jingliyang female 人力资源 jingliyang female 销售 jingliyang female 运营

2 内连接:只连接匹配的行

MysqL> select employee.ID,employee.name,employee.age,employee.sex,department.name employee inner join department on employee.dep_ID=+----+-----------+------+--------+--------------+| ID | name | age | sex | name |+----+-----------+------+--------+--------------+| 1 | egon | 18 | male | 技术 || 2 | alex | 48 | female | 人力资源 || 3 | wupeiqi | 38 | male | 人力资源 || 4 | yuanhao | 28 | female | 销售 || 5 | liwenzhou | 18 | male | 技术 |+----+-----------+------+--------+--------------+

<span >#<span >上述sql等同于
MysqL> select employee.ID,department.name <span >from employee,department where employee.dep_ID=department.ID;

3 外链接之左连接:优先显示左表全部记录

MysqL> select employee.ID,department.name as depart_name employee left join department on employee.dep_ID=+----+------------+--------------+| ID | name | depart_name |+----+------------+--------------+| 1 | egon | 技术 || 5 | liwenzhou | 技术 || 2 | alex | 人力资源 || 3 | wupeiqi | 人力资源 || 4 | yuanhao | 销售 || 6 | jingliyang | NulL |+----+------------+--------------+

4 外链接之右连接:优先显示右表全部记录

MysqL> select employee.ID,department.name as depart_name employee right join department on employee.dep_ID=+------+-----------+--------------+| ID | name | depart_name |+------+-----------+--------------+| 1 | egon | 技术 || 2 | alex | 人力资源 || 3 | wupeiqi | 人力资源 || 4 | yuanhao | 销售 || 5 | liwenzhou | 技术 || NulL | NulL | 运营 |+------+-----------+--------------+

5 全外连接:显示左右两个表全部记录

select * employee left join department on employee.dep_ID =* employee right join department on employee.dep_ID =+------+------------+--------+------+--------+------+--------------+| ID | name | sex | age | dep_ID | ID | name |+------+------------+--------+------+--------+------+--------------+| 1 | egon | male | 18 | 200 | 200 | 技术 || 5 | liwenzhou | male | 18 | 200 | 200 | 技术 || 2 | alex | female | 48 | 201 | 201 | 人力资源 || 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 || 4 | yuanhao | female | 28 | 202 | 202 | 销售 || 6 | jingliyang | female | 18 | 204 | NulL | NulL || NulL | NulL | NulL | NulL | NulL | 203 | 运营 |+------+------------+--------+------+--------+------+--------------+

<span >#<span >注意 union与union all的区别:union会去掉相同的纪录

二 符合条件连接查询select employee.name,employee.age = age > 25<span >#<span >示例2:以内连接的方式查询employee和department表,并且以age字段的升序方式显示
select employee.ID,department.name <span >from
<span > employee,department
where employee.dep_ID
=<span > department.ID
<span >and
age > 25<span >
order by age asc;

三 子查询 、<等1 带IN关键字的子查询select * department);2 带比较运算符的子查询、>=、<、<=、<>select ID,name employee group by dep_ID having avg(age) > 25<span >#<span >查看技术部员工姓名
select name <span >from
<span > employee
where dep_ID
<span >in
<span >
(select ID
<span >from
department where name=<span >'
<span >技术
<span >'
<span >);

<span >#<span >查看不足1人的部门名
select name <span >from<span > department
where ID <span >in<span >
(select dep_ID <span >from employee group by dep_ID having count(ID) <=1);

3 带EXISTS关键字的子查询

EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录。
而是返回一个真假值。True或False
当返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询

MysqL> select * ->-> (select ID department where ID=200+----+------------+--------+------+--------+| ID | name | sex | age | dep_ID |+----+------------+--------+------+--------+| 1 | egon | male | 18 | 200 || 2 | alex | female | 48 | 201 || 3 | wupeiqi | male | 38 | 201 || 4 | yuanhao | female | 28 | 202 || 5 | liwenzhou | male | 18 | 200 || 6 | jingliyang | female | 18 | 204 |+----+------------+--------+------+--------+

<span >#<span >department表中存在dept_ID=205,False
MysqL> select * <span >from<span > employee
-><span > where exists
-> (select ID <span >from department where ID=204<span >);
Empty set (0.00 sec)

四 综合练习

init.sql文件内容

/*Source Server : localhost
Source Server Type : MysqL
Source Server Version :
50624<span >
Source Host : localhost
Source Database : sqlexam

Target Server Type : MysqL
Target Server Version : 50624<span >
file EnCoding : utf-8<span >

Date: 10/21/2016 06:46:46<span > AM
*/<span >

SET nameS utf8;
SET FOREIGN_KEY_CHECKS =<span > 0;

-- ----------------------------
-- table structure <span >for <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;
-- ----------------------------<span >
DROP table IF EXISTS </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;;
CREATE table </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; (
c<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a> int(11<span >) NOT NulL auto_INCREMENT,caption varchar(32<span >) NOT NulL,PRIMARY KEY (c<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>)
) ENGINE=InnoDB auto_INCREMENT=5 DEFAulT CHARSET=<span >utf8;

-- ----------------------------
-- Records of <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;
-- ----------------------------<span >
BEGIN;
INSERT INTO </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;class</span> VALUES (<span >'<span >1<span >',<span >'<span >三年二班<span >'),(<span >'<span >2<span >',<span >'<span >三年三班<span >'),(<span >'<span >3<span >',<span >'<span >一年二班<span >'),(<span >'<span >4<span >',<span >'<span >二年九班<span >'<span >);
COMMIT;

-- ----------------------------
-- table structure <span >for<span > course
-- ----------------------------<span >
DROP table IF EXISTS course;
CREATE table course (
c<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a> int(11<span >) NOT NulL auto_INCREMENT,c<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> varchar(32<span >) NOT NulL,teacher_<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a> int(11<span >) NOT NulL,PRIMARY KEY (c<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>),KEY fk_course_teacher (teacher_<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>),CONSTRAINT fk_course_teacher FOREIGN KEY (teacher_<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>) REFERENCES teacher (t<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>)
) ENGINE=InnoDB auto_INCREMENT=5 DEFAulT CHARSET=<span >utf8;

-- ----------------------------
--<span > Records of course
-- ----------------------------<span >
BEGIN;
INSERT INTO course VALUES (<span >'<span >1<span >',<span >'<span >生物<span >',<span >'<span >1<span >'),<span >'<span >物理<span >',<span >'<span >2<span >'),<span >'<span >体育<span >',<span >'<span >3<span >'),<span >'<span >美术<span >',<span >'<span >2<span >'<span >);
COMMIT;

-- ----------------------------
-- table structure <span >for<span > <a href="https://www.jb51.cc/tag/score/" target="_blank" >score</a>
-- ----------------------------<span >
DROP table IF EXISTS <a href="https://www.jb51.cc/tag/score/" target="_blank" >score</a>;
CREATE table <a href="https://www.jb51.cc/tag/score/" target="_blank" >score</a> (
s<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a> int(11<span >) NOT NulL autoINCREMENT,`studentIDint(</span>11<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;) NOT N<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>L,course_IDint(</span>11<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;) NOT N<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>L,numint(</span>11<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;) NOT N<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>L,PRIMARY KEY (sID),KEYfk_scorestudent(studentID),KEYfk_scorecourse(courseID),CONSTRAINTfk_scorecourseFOREIGN KEY (courseID) REFERENCEScourse(cID),CONSTRAINTfk_scorestudentFOREIGN KEY (studentID) REFERENCESstudent(sID`)
) ENGINE=InnoDB auto_INCREMENT=53 DEFAulT CHARSET=<span >utf8;

-- ----------------------------
--<span > Records of <a href="https://www.jb51.cc/tag/score/" target="_blank" >score</a>
-- ----------------------------<span >
BEGIN;
INSERT INTO <a href="https://www.jb51.cc/tag/score/" target="_blank" >score</a> VALUES (<span >'<span >1<span >',<span >'<span >1<span >',<span >'<span >10<span >'),<span >'<span >2<span >',<span >'<span >9<span >'),(<span >'<span >5<span >',<span >'<span >4<span >',<span >'<span >66<span >'),(<span >'<span >6<span >',<span >'<span >8<span >'),(<span >'<span >8<span >',<span >'<span >3<span >',<span >'<span >68<span >'),(<span >'<span >9<span >',<span >'<span >99<span >'),(<span >'<span >10<span >',<span >'<span >77<span >'),(<span >'<span >11<span >',(<span >'<span >12<span >',<span >'<span >87<span >'),(<span >'<span >13<span >',(<span >'<span >14<span >',<span >'<span >79<span >'),(<span >'<span >15<span >',<span >'<span >11<span >'),(<span >'<span >16<span >',<span >'<span >67<span >'),(<span >'<span >17<span >',<span >'<span >100<span >'),(<span >'<span >18<span >',<span >'<span >5<span >',(<span >'<span >19<span >',(<span >'<span >20<span >',(<span >'<span >21<span >',(<span >'<span >22<span >',<span >'<span >6<span >',(<span >'<span >23<span >',(<span >'<span >24<span >',(<span >'<span >25<span >',(<span >'<span >26<span >',<span >'<span >7<span >',(<span >'<span >27<span >',(<span >'<span >28<span >',(<span >'<span >29<span >',<span >'<span >88<span >'),(<span >'<span >30<span >',<span >'<span >8<span >',(<span >'<span >31<span >',(<span >'<span >32<span >',(<span >'<span >33<span >',(<span >'<span >34<span >',<span >'<span >9<span >',<span >'<span >91<span >'),(<span >'<span >35<span >',(<span >'<span >36<span >',(<span >'<span >37<span >',<span >'<span >22<span >'),(<span >'<span >38<span >',<span >'<span >10<span >',<span >'<span >90<span >'),(<span >'<span >39<span >',(<span >'<span >40<span >',<span >'<span >43<span >'),(<span >'<span >41<span >',(<span >'<span >42<span >',<span >'<span >11<span >',(<span >'<span >43<span >',(<span >'<span >44<span >',(<span >'<span >45<span >',(<span >'<span >46<span >',<span >'<span >12<span >',(<span >'<span >47<span >',(<span >'<span >48<span >',(<span >'<span >49<span >',(<span >'<span >52<span >',<span >'<span >13<span >',<span >'<span >87<span >'<span >);
COMMIT;

-- ----------------------------
-- table structure <span >for<span > student
-- ----------------------------<span >
DROP table IF EXISTS student;
CREATE table student (
s<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a> int(11<span >) NOT NulL auto_INCREMENT,gender char(1<span >) NOT NulL,class_<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a> int(11<span >) NOT NulL,s<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> varchar(32<span >) NOT NulL,KEY fk_class (class_<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>),CONSTRAINT fk_class FOREIGN KEY (class_<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>) REFERENCES </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; (c<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>)
) ENGINE=InnoDB auto_INCREMENT=17 DEFAulT CHARSET=<span >utf8;

-- ----------------------------
--<span > Records of student
-- ----------------------------<span >
BEGIN;
INSERT INTO student VALUES (<span >'<span >1<span >',<span >'<span >男<span >',<span >'<span >理解<span >'),<span >'<span >女<span >',<span >'<span >钢蛋<span >'),<span >'<span >张三<span >'),<span >'<span >张一<span >'),<span >'<span >张二<span >'),<span >'<span >张四<span >'),(<span >'<span >7<span >',<span >'<span >铁锤<span >'),<span >'<span >李三<span >'),<span >'<span >李一<span >'),<span >'<span >李二<span >'),<span >'<span >李四<span >'),<span >'<span >如花<span >'),<span >'<span >刘三<span >'),<span >'<span >刘一<span >'),<span >'<span >刘二<span >'),<span >'<span >刘四<span >'<span >);
COMMIT;

-- ----------------------------
-- table structure <span >for<span > teacher
-- ----------------------------<span >
DROP table IF EXISTS teacher;
CREATE table teacher (
t<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a> int(11<span >) NOT NulL auto_INCREMENT,t<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> varchar(32<span >) NOT NulL,PRIMARY KEY (t<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>)
) ENGINE=InnoDB auto_INCREMENT=6 DEFAulT CHARSET=<span >utf8;

-- ----------------------------
--<span > Records of teacher
-- ----------------------------<span >
BEGIN;
INSERT INTO teacher VALUES (<span >'<span >1<span >',<span >'<span >张磊老师<span >'),<span >'<span >李平老师<span >'),<span >'<span >刘海燕老师<span >'),<span >'<span >朱云海老师<span >'),<span >'<span >李杰老师<span >'<span >);
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;

从init.sql文件中导入数据

MysqL>>> source /root/init.sql

!!!重中之重:练习之前务必搞清楚sql逻辑查询语句的执行顺序

http://www.cnblogs.com/huchong/p/7515787.html

12<span >、查询学生表中男女生各有多少人

3<span >、查询物理成绩等于100的学生的姓名

4<span >、查询平均成绩大于八十分的同学的姓名和平均成绩

5<span >、查询所有学生的学号,姓名,选课数,总成绩

6<span >、 查询姓李老师的个数

7<span >、 查询没有报李平老师课的学生姓名

8<span >、 查询物理课程比生物课程高的学生的学号

9<span >、 查询没有同时选修物理课程和体育课程的学生姓名

10<span >、查询挂科超过两门(包括两门)的学生姓名和班级

11<span > 、查询选修了所有课程的学生姓名

12<span >、查询李平老师教的课程的所有成绩记录

13<span >、查询全部学生都选修了的课程号和课程名

14<span >、查询每门课程被选修的次数

15<span >、查询之选修了一门课程的学生姓名和学号

16<span >、查询所有学生考出的成绩并按从高到低排序(成绩去重)

17<span >、查询平均成绩大于85的学生姓名和平均成绩

18<span >、查询生物成绩不及格的学生姓名和对应生物分数

19<span >、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名

20<span >、查询每门课程成绩最好的前两名学生姓名

21<span >、查询不同课程但成绩相同的学号,课程号,成绩

22<span >、查询没学过“叶平”老师课程的学生姓名以及选修的课程名称;

23<span >、查询所有选修了学号为1的同学选修过的一门或者多门课程的同学学号和姓名;

24、任课最多的老师中学生单科成绩最高的学生姓名

1 course left join teacher on course.teacher_ID=23select sname student where sID = (select cID course where cname=) num=100select sname = (select cID course where cname=) num=100=4<span >、查询平均成绩大于八十分的同学的姓名和平均成绩
select student.sname,t1.平均成绩
<span >from
<span > student inner join
(select student_ID,avg(num) 平均成绩
<span >from
score group by student_ID having avg(num) > 80<span >) as t1
on student.sID
=<span >t1.student_ID;
5<span >、查询所有学生的学号,姓名,选课数,总成绩
select student.sID,sname 学生名,选课数,总成绩
<span >from
<span > student left join
(select student_ID,count(course_ID) 选课数,sum(num) 总成绩
<span >from
<span > score group by student_ID) as t1
on student.sID
=<span >t1.student_ID
;
6<span >、 查询姓李老师的个数
select count(tID)
<span >from
teacher where tname like <span >'
<span >李%
<span >'
<span >;

7<span >、 查询没有报李平老师课的学生姓名
select sname <span >from student where sID <span >not <span >in<span > (
select distinct student_ID <span >from score where course_ID <span >in<span > (
select cID <span >from course where teacher_ID=(select tID <span >from teacher where tname=<span >'<span >李平老师<span >'<span >)
)
);
8<span >、 查询物理课程比生物课程高的学生的学号
select t1.student_ID <span >from<span >
(select student_ID,num <span >from score inner join course on score.course_ID=<span >course.cID
where course.cname=<span >'<span >物理<span >'<span >) as t1
inner join
(select student_ID,num <span >from score inner join course on score.course_ID=<span >course.cID
where course.cname=<span >'<span >生物<span >'<span >) as t2
on t1.student_ID=<span >t2.student_ID
where t1.num ><span > t2.num
;
9<span >、 查询没有同时选修物理课程和体育课程的学生姓名
select sname <span >from student where sID <span >in<span > (
select student_ID <span >from<span > score inner join course
on course.cname <span >in (<span >'<span >物理<span >',<span >'<span >体育<span >') <span >and course.cID=<span >score.course_ID
group by student_ID having count(course_ID) !=2<span >
);
10<span >、查询挂科超过两门(包括两门)的学生姓名和班级名字
select t2.sname,<span >class.caption <span >from<span >
(select sname,class_ID <span >from<span > student inner join (
select student_ID <span >from<span > score
where num < 60 group by student_id having count(course_id) >=2<span >
) as t1
on student.sID=<span >t1.student_ID) as t2
inner join <span >class<span >
on <span >class.cID =<span > t2.class_ID
;
11<span > 、查询选修了所有课程的学生姓名
select sname <span >from<span > student inner join
(
select student_ID <span >from score group by student_ID having count(course_ID) = (select count(cID) <span >from<span > course)
) t1
on t1.student_ID =<span > student.sID
;
12<span >、查询李平老师教的课程的所有成绩记录
select student_ID,course_ID,num <span >from<span > score inner join
(
select cID <span >from course inner join teacher on teacher.tname=<span >'<span >李平老师<span >' <span >and teacher.tID=<span >course.teacher_ID
) as t1
on t1.cID=<span >score.course_ID
;

13<span >、查询全部学生都选修了的课程号和课程名
select course.cID,course.cname <span >from<span > course inner join
(
select course_ID <span >from<span > score group by course_ID
having count(student_ID) = (select count(sID) <span >from<span > student)
) t1
on t1.course_ID=<span >course.cID
;

14<span >、查询每门课程被选修的次数
select course.cname,选修人数 <span >from<span > course inner join
(
select course_ID,count(student_ID) as 选修人数 <span >from<span > score group by course_ID
) as t1
on t1.course_ID=<span >course.cID
;
15<span >、查询之选修了一门课程的学生姓名和学号
select sID,sname <span >from<span > student inner join
(
select student_ID <span >from score group by student_ID having count(course_ID)=1<span >
) t1
on t1.student_ID =<span > student.sID
;
16<span >、查询所有学生考出的成绩并按从高到低排序(成绩去重)
select distinct num <span >from<span > score order by num desc;

17<span >、查询平均成绩大于85的学生姓名和平均成绩
select student.sname,avg_num <span >from<span > student inner join
(
select student_ID,avg(num) as avg_num <span >from score group by student_ID having avg(num) > 85<span >
) t1
on student.sID=<span >t1.student_ID
;

18<span >、查询生物成绩不及格的学生姓名和对应生物分数
select student.sname,t1.num <span >from<span > student inner join
(
select student_ID,num <span >from<span > score
where course_ID=(select cID <span >from course where cname=<span >'<span >生物<span >') <span >and num < 60<span >
) t1
on t1.student_ID=<span >student.sID
;
19<span >、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名

select sname <span >from student where sID =<span >
(
select student_ID <span >from score where course_ID <span >in<span >
(
select cID <span >from course inner join teacher on teacher.tname=<span >'<span >李平老师<span >' <span >and course.teacher_ID=<span >teacher.tID
)
group by student_ID
order by avg(num) desc
limit 1<span >
);

总结

以上是内存溢出为你收集整理的多表查询全部内容,希望文章能够帮你解决多表查询所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1169505.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存