加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql 多表连接查询

发布时间:2020-12-12 09:36:57 所属栏目:MsSql教程 来源:网络整理
导读:新建两张表:表1:student 截图如下: IMG alt=SQL多表连接查询 src="https://files.jb51.cc/file_images/article/201309/201309281241482.jpg"gt;表2:course 截图如下:IMG alt=SQL多表连接查询 src="https://files.jb51.cc/file_images/article/201309/20

新建两张表:表1:student 截图如下:

<IMG alt=SQL多表连接查询 src="https://files.52php.cn/file_images/article/201309/201309281241482.jpg"&gt;表2:course 截图如下:<IMG alt=SQL多表连接查询 src="https://files.52php.cn/file_images/article/201309/201309281241483.jpg"&gt;(此时这样建表只是为了演示连接SQL语句,当然实际开发中我们不会这样建表,实际开发中这两个表会有自己不同的主键。)

一、外连接


外连接可分为:左连接、右连接、完全外连接。1、左连接 left join 或 left outer joinSQL语句:select from student left join course on student.ID=course.ID
执行结果:

<IMG alt=SQL多表连接查询 src="https://files.52php.cn/file_images/article/201309/201309281241484.jpg"&gt;左外连接包含left join左表所有行,如果左表中某行在右表没有匹配,则结果中对应行右表的部分全部为空(NULL).注:此时我们不能说结果的行数等于左表数据的行数。当然此处查询结果的行数等于左表数据的行数,因为左右两表此时为一对一关系。2、右连接 right join 或 right outer join

SQL语句:select
from student right join course on student.ID=course.ID
执行结果:

<IMG alt=SQL多表连接查询 src="https://files.52php.cn/file_images/article/201309/201309281241485.jpg"&gt;右外连接包含right join右表所有行,如果左表中某行在右表没有匹配,则结果中对应左表的部分全部为空(NULL)。注:同样此时我们不能说结果的行数等于右表的行数。当然此处查询结果的行数等于左表数据的行数,因为左右两表此时为一对一关系。3、完全外连接 full join 或 full outer joinSQL语句:select from student full join course on student.ID=course.ID

执行结果:

<IMG alt=SQL多表连接查询 src="https://files.52php.cn/file_images/article/201309/201309281241486.jpg"&gt;完全外连接包含full join左右两表中所有的行,如果右表中某行在左表中没有匹配,则结果中对应行右表的部分全部为空(NULL),如果左表中某行在右表中没有匹配,则结果中对应行左表的部分全部为空(NULL)。

二、内连接 join或 inner join

SQL语句:select
from student inner join course on student.ID=course.ID
执行结果:

<IMG alt=SQL多表连接查询 src="https://files.52php.cn/file_images/article/201309/201309281241487.jpg"&gt;inner join 是比较运算符,只返回符合条件的行。此时相当于:select from student,course where student.ID=course.ID

三、交叉连接 cross join


1.概念:没有 WHERE 子句的交叉联接将产生连接所涉及的表的笛卡尔积。第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。SQL语句:select
from student cross join course
执行结果:

<IMG alt=SQL多表连接查询 src="https://files.52php.cn/file_images/article/201309/201309281241488.jpg"&gt;如果我们在此时给这条SQL加上WHERE子句的时候比如SQL:select from student cross join course where student.ID=course.ID此时将返回符合条件的结果集,结果和inner join所示执行结果一样。

四、两表关系为一对多,多对一或多对多时的连接语句


当然上面两表为一对一关系,那么如果表A和表B为一对多、多对一或多对多的时候,我们又该如何写连接SQL语句呢?其实两表一对多的SQL语句和一对一的SQL语句的写法都差不多,只是查询的结果不一样,当然两表也要略有改动。比如表1的列可以改为:
Sno Name Cno
表2的列可以改为:
Cno CName
这样两表就可以写一对多和多对一的SQL语句了,写法和上面的一对一SQL语句一样。下面介绍一下当两表为多对多的时候我们该如何建表以及些SQL语句。新建三表:表A: student 截图如下:

<IMG alt=SQL多表连接查询 src="https://files.52php.cn/file_images/article/201309/201309281241489.jpg"&gt;表B: course 截图如下:<IMG alt=SQL多表连接查询 src="https://files.52php.cn/file_images/article/201309/2013092812414810.jpg"&gt;表C: student_course 截图如下:<IMG alt=SQL多表连接查询 src="https://files.52php.cn/file_images/article/201309/2013092812414811.jpg"&gt;一个学生可以选择多门课程,一门课程可以被多个学生选择,因此学生表student和课程表course之间是多对多的关系。当两表为多对多关系的时候,我们需要建立一个中间表student_course,中间表至少要有两表的主键,当然还可以有别的内容。SQL语句:select s.Name,C.Cname from student_course as sc left join student as s on s.Sno=sc.Sno left join course as c on c.Cno=sc.Cno执行结果:<IMG alt=SQL多表连接查询 src="https://files.52php.cn/file_images/article/201309/2013092812414812.jpg"&gt;此条SQL执行的结果是学生选课的情况。 <FONT style="COLOR: #ff0000">以下是特色补充:

select
from (SELECT id,jihao,ht.hetongid,hetonghao,banshichu,zulindanwei,jiansheluduan,shebeijinchangriqi,hetongfangshi,
jine,yusuangongchengliang,(ht.jine/30)(datediff(day,ht.shebeijinchangriqi,getdate())) as yusuanjine,yihuikuanjine,
((ht.jine/30)
(datediff(day,getdate()))-yihuikuanjine) as qiankuanjine,ht.shebeituichangriqi
FROM db_hetongguanli_y_g as ht left join select sum (gongchengliang) as yusuangongchengliang,hetongid
from db_meirigongchengliang group by hetongid) as d on ht.hetongid=d.hetongid left
join(select sum(shijihuikuanjine) as yihuikuanjine,hetongid from db_huikuan group by hetongid) as a on ht.hetongid=a.hetongid ) as b
where b.hetongfangshi='月租' and b.hetongid=(select hetongid from db_shebeixinxi where jihao='" + this.DropDownList1.SelectedValue + "')"; ================================================================================================== select * from (select ht.id,ht.qiandingren,ht.banshichu,ht.jihao,ht.hetonghao,ht.zulindanwei,
ht.jiansheluduan,ht.qingqianriqi,ht.lumianshigongcailiao,ht.hetongfangshi,ht.jine,ht.hetongqiandingriqi,
ht.hetongjiesuanjine,ht.hetongjiesuanriqi,ht.shebeituichangriqi,ht.jiesuangongchengliang,
d.yifukuanjine,(ht.hetongjiesuanjine-d.yifukuanjine) as qiankuanjine,ht.kaipiaojine,ht.ranyoufei,ht.yunfei,
ht.qitafeiyong,ht.feiliaofei from db_hetongguanli_y_g as ht left join (select hetongid,sum(shijihuikuanjine) as yifukuanjine
from db_huikuan group by hetongid) as d on ht.hetongid=d.hetongid) as a
where a.hetongid=(select hetongid from db_shebeixinxi where jihao='5666')
go

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读