!!!!!多表查询有限制的 SELECT * from a left join b on a.id=b.id where 条件
或者 SELECT * from a where ID in (SELECT * from b where 条件)
SELECT SC.* from SC
SELECT * from Course
分页:
SELECT * from Course limit 0,3
SELECT * from Course limit 3,3
SELECT * from Course limit 6,3
多表连接查询
一、等值与非等值连接查询
SELECT * from student;
SELECT * from Course;
SELECT * from SC;
1 数据库
2 数学
3 信息系统
迪卡尔积
select * from Student ,SC
select * from Student ,SC where Student.Sno=sc.Sno
select * from Student ,SC where Student.Sno<>sc.Sno
select A.*,B.Cno,B.Grade from Student A ,SC B where A.Sno=B.Sno
select A.*,B.Cno,C.Cname,B.Grade from Student as A,SC as B,Course as C
where A.Sno=B.Sno and B.Cno=C.Cno
select A.*,C.Cname,B.Grade from Student as A,SC as B,Course as C
where A.Sno=B.Sno and B.Cno=C.Cno
--》***
select A.Sno,A.Sname,Ssex,C.Cname,B.Grade from Student as A,SC as B,Course as C
where A.Sno=B.Sno and B.Cno=C.Cno
二、自身连接
SELECT * from Course;
SELECT * from Course A,Course B;
-- 直接先修课
SELECT A.Cno,A.Cname ,A.Cpno,B.Cname from Course A,Course B
where A.Cpno=B.Cno and A.Cno=1
1 数据库 5 数据结构
-- 间接先修课
SELECT A.Cno,A.Cname ,B.cpno from Course A,Course B
where A.Cpno=B.Cno and A.Cno=1
1 数据库 7
SELECT FIRST.Cno,SECOND.Cpno
FROM Course FIRST,Course SECOND
WHERE FIRST.Cpno= SECOND.Cno;
==>1 数据库 7 PASCAL语言
SELECT A.Cno,A.Cname ,B.cpno,C.Cname from Course A,Course B ,Course C
where A.Cpno=B.Cno and B.Cpno=C.Cno and A.Cno=1
==>查询每一门课的间接先修课
SELECT A.Cno,A.Cname ,B.cpno,C.Cname from Course A,Course B ,Course C
where A.Cpno=B.Cno and B.Cpno=C.Cno
1 数据库 7 PASCAL语言
3 信息系统 5 数据结构
5 数据结构 6 数据处理
三、外连接
--3.1查找每个学生的选课情况
SELECT * from student;
SELECT * from Course;
SELECT * from SC;
--left join: 以左表为基准,和右边的表连接,右边记录没有的以null代替。
SELECT * from student A left outer join Sc B on A.Sno=B.Sno;
--***
SELECT * from student A left join Sc B on A.Sno=B.Sno;
--3.2查找选过课程的学生的选课情况
SELECT * from student A right join Sc B on A.Sno=B.Sno;
SELECT * from Sc A left join student B on A.Sno=B.Sno;
---3.3 查找每个学生的选课情况,要显示课程名称, 3表
SELECT * from student A left join Sc B on A.Sno=B.Sno;
-----> 三表
SELECT A.*,B.*,C.* from student A LEFT JOIN Sc B on A.Sno=B.Sno LEFT JOIN Course C B.Cno=C.cno;
---->三表 INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
SELECT A.*,B.*,C.* from student A JOIN Sc B JOIN Course C on A.Sno=B.Sno and B.Cno=C.cno;
SELECT A.*,B.Cno,C.Cname,B.Grade from student A inner JOIN Sc B inner JOIN Course C on A.Sno=B.Sno and B.Cno=C.cno;
200215121 李勇 男 20 CS 1 数据库 92
200215121 李勇 男 20 CS 2 数学 85
200215121 李勇 男 20 CS 3 信息系统 88
200215122 刘晨 女 18 CS 2 数学 90
200215122 刘晨 女 18 CS 3 信息系统 80
JOIN 按照功能大致分为如下三类:
=======================================================================
***** INNER JOIN/join(内连接,或等值连接):获取两个表中字段匹配关系的记录。
***** LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。
=================2表======================================================
--left join: 以左表为基准,和右边的表连接,右边记录没有的以null代替。
SELECT * from student A left outer join Sc B on A.Sno=B.Sno;
=================3表 连接 显示Cname
SELECT * FROM student A LEFT OUTER JOIN sc B
ON A.Sno = D.Sno LEFT JOIN course C on B.Cno = C.Cno
============================================
200215121 李勇 男 20 CS 200215121 1 数据库 92
200215121 李勇 男 20 CS 200215121 2 数学 85
200215121 李勇 男 20 CS 200215121 3 信息系统 88
200215122 刘晨 女 18 CS 200215122 2 数学 90
200215122 刘晨 女 18 CS 200215122 3 信息系统 80
200215123 王敏 女 18 MA
200215125 张立 男 19 IS
四、复合条件连接
嵌套查询 IN
SELECT * from Student;
SELECT * from Student where sage in (18,19);
男生的选课情况:
select * from SC where Sno in (SELECT sno from Student where ssex = '男');
select * from SC where Sno in (SELECT sno from Student where ssex in ('男'));