标签:
笛卡尔
-------------笛卡尔积------------------
select * from StuInfo,StuScore
select * from StuInfo cross join StuScore
--查询出有意义的行
select * from StuInfo as a cross join StuScore as b
where a.StuNo = b.StuNo
--------------内联接-------------------
--作用:用来查询多个表中符合联接条件的数据
--语法: inner join......on.....
select * from StuInfo inner join StuScore
on StuInfo.StuNo = StuScore.StuNo
--注意:inner可以省略,on后面接的是连接条件
--查询有及格的学号、姓名、科目、成绩
select a.StuNo,a.StuName,b.subject,b.score
from StuInfo as a inner join StuScore as b
on a.StuNo = b.StuNo where b.score>=60
--------------外联接---------------------
--左外联接: left join....on.....
--作用:将左表的数据全部显示出来,右表中符合条件的就显示,其他的都用NULL填充
select * from StuInfo left join StuScore
on StuInfo.StuNo = StuScore.StuNo
--右外联接: right join....on....
--作用:将右表的数据全部显示出来,左表中符合条件的就显示,其他的都用NULL填充
select * from StuInfo right join StuScore
on StuInfo.StuNo = StuScore.StuNo
--完全联接(左外联接+右外联接)
--作用:左表、右表中的数据都全部显示出来,不符合联接条件的就用NULL填充
--语法:full join....on....
select * from StuInfo full join StuScore
on StuInfo.StuAge = StuScore.StuNo
-------------交集intersect----------------
--查询共有的行
(select ‘张三‘,23 union
select ‘李四‘,24)
intersect
(select ‘张三‘,23 union
select ‘王五‘,25 )
------------差集except------------------
--返回左数据集中有的但在右数据集中没有的行
(select ‘张三‘,23 union
select ‘李四‘,24)
except
(select ‘张三‘,23 union
select ‘王五‘,25 )
--------------案例1---------------
--identity(int,1,1)添加标识列
select A,identity(int,1,1)
as 标识列 into tblTemp from TBL
--奇数行汇总
select sum(A) from tblTemp where 标识列%2<>0
--偶数行汇总
select sum(A) from tblTemp where 标识列%2=0
---------------案例2----------------
insert into Account
select Users.CardID,2 from Users left join Account
on Users.CardID=Account.CardID
where Account.CardID is null
标签:
原文地址:http://www.cnblogs.com/danmao/p/3870779.html