SQLServer中怎么实现多表连接查询功能-创新互联
SQL Server中怎么实现多表连接查询功能 ,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联是一家专注于网站设计制作、成都网站制作与策划设计,禹王台网站建设哪家好?创新互联做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:禹王台等地区。禹王台做网站价格咨询:18982081108具体代码如下所示:
-- 交叉连接产生笛卡尔值 (X*Y)SELECT *FROM Student cross Join dbo.ClassInfo --另外一种写法SELECT *FROM Student , ClassInfo -- 内连接 (Inner 可以省略)SELECT *FROM Student JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;-- Inner JoinSELECT *FROM Student INNER JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID; -- on 条件,通常是 主外键,但是不限于主外键 -- on 条件,允许有多个,而且可能是针对某个表的SELECT *FROM Student INNER JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID AND dbo.Student.Class IN (1,2) -- 针对Student表增加查询条件-- 不等于 (笛卡尔值减去 相等的值)SELECT *FROM Student INNER JOIN dbo.ClassInfo ON dbo.Student.Class <> dbo.ClassInfo.ID;--自连接 从Class表中查询到Class所在的系SELECT T1.* ,T2.ClassName FROM dbo.ClassInfo AS T1 INNER JOIN dbo.ClassInfo AS T2 ON T1.PID=T2.ID-- Left JoinSELECT *FROM Student Left JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;-- Right JoinSELECT *FROM Student RIGHT JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;-- 全外连接SELECT *FROM Student FULL OUTER JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;-- 外连接和内连接的区别是:-- 内连接,on条件不符合的话,会过滤掉数据-- 外连接,以保留表为主,on条件成立显示数据,否则显示NULL--Union 纵向连接SELECT StuID,StuName,StuEnName,StuAge,StuBirthdayFROM Student WHERE StuID<=2UNIONSELECT StuID,StuName,StuEnName,StuAge,StuBirthdayFROM dbo.Student WHERE StuID>2--Union 去重复SELECT StuSexFROM Student WHERE StuID<=2UNIONSELECT StuSexFROM dbo.Student WHERE StuID>2--Union 显示全部SELECT StuSexFROM Student WHERE StuID<=2UNION ALLSELECT StuSexFROM dbo.Student WHERE StuID>2--Except 差集,排除SELECT StuID,StuName,StuEnName,StuAge,StuBirthdayFROM StudentExceptSELECT StuID,StuName,StuEnName,StuAge,StuBirthdayFROM dbo.Student WHERE StuID<=2--Intersect 交集SELECT StuID,StuName,StuEnName,StuAge,StuBirthdayFROM Student WHERE StuID>=2ExceptSELECT StuID,StuName,StuEnName,StuAge,StuBirthdayFROM dbo.Student WHERE StuID<=3
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。
网页题目:SQLServer中怎么实现多表连接查询功能-创新互联
转载注明:http://ybzwz.com/article/jjeoo.html