SQL JOIN 五种查询

 use boris
 IF OBJECT_ID('Student') IS NOT NULL
DROP TABLE Student
CREATE TABLE  Student
(Sname nvarchar(50) null, Ssex nvarchar(50) null,Sage int null)
insert into Student
select '张三','男','14' union all
select '李四','女','15'union all
select '王五','男','16'
--select * from Student
 if OBJECT_ID('S_otherInfo') IS NOT NULL
 DROP TABLE S_otherInfo
 create table S_otherInfo(Sname nvarchar(50) not null,Adress nvarchar(50) null,Othername nvarchar(50) null)
 insert into S_otherInfo
 SELECT '赵六','天津','六' union all
 select '张三','上海','三' union all
 select '王五','海南','五'
--select * from S_otherInfo
--只显示符合条件的记录
select * FROM Student S inner JOIN S_otherInfo ON  S.Sname=S_otherInfo.Sname
--显示左边表所有的记录,以及右边表中符合条件的记录
SELECT * FROM Student S LEFT JOIN  S_otherInfo ON  S.Sname=S_otherInfo.Sname
--显示右边表所有的记录,以及左边表中符合条件的记录
SELECT * FROM Student S RIGHT JOIN  S_otherInfo ON  S.Sname=S_otherInfo.Sname
--显示所有表的记录,包括符合条件的记录和不符合条件的记录
SELECT * FROM Student S FULL JOIN S_otherInfo ON  S.Sname=S_otherInfo.Sname
--将一个表的每一条记录和另一个表中的每一条记录搭配成新的记录,不需要用ON来设置条件
SELECT * FROM Student S CROSS JOIN  S_otherInfo ORDER BY s.Sname 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值