首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请问 join 的不同写法

2012-04-14 
请教 join 的不同写法SQL code--判断如果系统里存在t_EmployeeBasicInfo表则删除该表if OBJECT_ID(t_Em

请教 join 的不同写法

SQL code
--判断如果系统里存在't_EmployeeBasicInfo'表则删除该表if OBJECT_ID('t_EmployeeBasicInfo','U') is not  null drop table t_EmployeeBasicInfogo--创建t_EmployeeBasicInfo表create table t_EmployeeBasicInfo(    ID varchar(20),    Name varchar(50),    Department varchar(100))go--插入数据insert into t_EmployeeBasicInfoselect 'ID0001','Andy','IT' union allselect 'ID0002','Job','BIO' union allselect 'ID0003','Jack','BEM' go--判断如果系统里存在't_EmployeeEducation'表则删除该表if OBJECT_ID('t_EmployeeEducation','U') is not  null drop table t_EmployeeEducationgo--创建t_EmployeeEducation表create table t_EmployeeEducation(    ID varchar(20),    University varchar(50),    Major varchar(50))go--插入数据insert into t_EmployeeEducationselect 'ID0002','东南大学','Business' union allselect 'ID0003','华东师范','Software Development' union allselect 'ID0004','复旦大学','Medical professional'go--查询't_EmployeeBasicInfo'表数据select * from t_EmployeeBasicInfogo--查询't_EmployeeEducation'表数据select * from t_EmployeeEducationgo--左连接 写法1select    A.ID,        A.Name,        A.Department,        B.ID,        B.University,        B.Majorfrom t_EmployeeBasicInfo Aleft join t_EmployeeEducation B on A.ID=B.ID--左连接 写法2/*  请写出其他写法 */go--右连接 写法1select    A.ID,        A.Name,        A.Department,        B.ID,        B.University,        B.Majorfrom t_EmployeeBasicInfo Aright join t_EmployeeEducation B on A.ID=B.ID--右连接 写法2/*  请写出其他写法 */go--内连接 写法1select    A.ID,        A.Name,        A.Department,        B.ID,        B.University,        B.Majorfrom t_EmployeeBasicInfo Ainner join t_EmployeeEducation B on A.ID=B.ID--内连接 写法2 select    A.ID,        A.Name,        A.Department,        B.ID,        B.University,        B.Majorfrom t_EmployeeBasicInfo A,t_EmployeeEducation B where A.ID=B.IDgo--外连接 写法1 select    A.ID,        A.Name,        A.Department,        B.ID,        B.University,        B.Majorfrom t_EmployeeBasicInfo Afull join t_EmployeeEducation B on A.ID=B.ID--外连接 写法2/*  请写出其他写法 */go--交叉连接 写法1select    A.ID,        A.Name,        A.Department,        B.ID,        B.University,        B.Majorfrom t_EmployeeBasicInfo Across join t_EmployeeEducation B order by A.ID--交叉连接 写法2/*  请写出其他写法 */


[解决办法]
很久以前有过这样的写法,但是貌似SQL SERVER 2000开始就不支持了。

SQL code
--很久以前的链接查询是这样的  --右连接 SELECT Student.sno ,        Sname ,        Ssex ,        Sage ,        Cno ,        Grade FROM   Student ,        SC WHERE  Student.Sno = SC.Sno(*) ;   --左连接:  SELECT Student.sno ,        Sname ,        Ssex ,        Sage ,        Cno ,        Grade FROM   Student ,        SC WHERE  Student.Sno(*) = SC.Sno ;
[解决办法]
SQL code
select    A.ID,        A.Name,        A.Department,        B.ID,        B.University,        B.Majorfrom t_EmployeeBasicInfo A, t_EmployeeEducation B where A.ID*=B.ID
[解决办法]
cross join 的等价于 

SQL code
select    A.ID,        A.Name,        A.Department,        B.ID,        B.University,        B.Majorfrom t_EmployeeBasicInfo A, t_EmployeeEducation B order by A.ID
[解决办法]
sql2000后就不支持了哦。很久之前的方法了
[解决办法]
最好还是使用规范写法,楼主给出的都是,其他不规范的,要么早就被淘汰,要么下一个版本就不支持了,没必要去学习了。
另外,楼主所说的其他写法,是不是说不用join,而且改写成使用子查询等方法?

[解决办法]
楼主写的这些,很全了。

热点排行