分享

连接查询

 Ethan的博客 2011-03-30
create database test go create table t1 (deptid int,uname char(10)) create table t2 (deptid int,memo char(50)) insert into t1 values(1,'John') insert into t1 values(2,'Tom') insert into t1 values(3,'Michal') insert into t2 values(1,'Human Resources Department') insert into t2 values(2,'General Accounting Department') insert into t2 values(4,'Engineering Department') insert into t2 values(5,'Sales Department') 下面依次运行下列语句: select * from t1,t2 where t1.deptid=t2.deptid select * from t1 inner join t2 on t1.deptid=t2.deptid select * from t1 left join t2 on t1.deptid=t2.deptid select * from t1 right join t2 on t1.deptid=t2.deptid select * from t1 full join t2 on t1.deptid=t2.deptid select * from t1 cross join t2 语句1和2结果均为: deptid uname deptid memo 1 John 1 Human Resources Department 2 Tom 2 General Accounting Department 可见内联接与where子句效果一样。 语句3(左联)运行结果为: deptid uname deptid memo 1 John 1 Human Resources Department 2 Tom 2 General Accounting Department 3 Michael NULL NULL 语句4(右联)运行结果为: deptid uname deptid memo 1 John 1 Human Resources Department 2 Tom 2 General Accounting Department NULL NULL 4 Engineering Department NULL NULL 5 Sales Department 语句5(全联)运行结果为: deptid uname deptid memo 1 John 1 Human Resources Department 2 Tom 2 General Accounting Department NULL NULL 4 Engineering Department NULL NULL 5 Sales Department 3 Michael NULL NULL 语句6(交叉联接,注意该语句是没有ON子句的!)运行结果为: deptid uname deptid  memo  1   John     1    Human Resources Department  1   John    2    General Accounting Department  1   John    4    Engineering Department  1   John    5    Sales Department  2   Tom   1    Human Resources Department  2   Tom   2    General Accounting Department  2   Tom   4    Engineering Department  2   Tom   5    Sales Department  3   Michael   1    Human Resources Department  3   Michael   2    General Accounting Department  3   Michael   4    Engineering Department  3   Michael   5    Sales Department

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多