日期:2014-05-18  浏览次数:20394 次

左外关联的语法比较。
SQL code

select b.kid, a.type from a, b where b.kid*=a.id  --带*

select b.kid, a.type from a, b where b.kid=a.id  --不带*

select tb1.* from tb1 left outer join tb2 on tb1.col1=tb2.col1 --join



以上三个语句都能正常访问。

请问他们的区别各是什么,有什么优劣 和 缺陷。

C# code
{第一种*的存在不知道是为了什么。}

C# code
{曾听说 第二种方法有时不能正确显示,其中原因不明。}

C# code
{第三种是常用的join,一般都这么进行做外关联查询。}


------解决方案--------------------
1、为了兼容之前的语法. *= 这个是在2000之前广泛应用的
2、? 没有碰到不能正确显示的问题。 
3、这还得取决于LZ的需求,如果一定要相等id的数据,那么inner join 会是第一选择。



------解决方案--------------------
SQL code

if object_id('[a]') is not null drop table [a]
create table [a] (id int,type varchar(1))
insert into [a]
select 1,'a' union all
select 2,'b' union all
select 3,'c'

if object_id('[b]') is not null drop table [b]
create table [b] (kid int,col varchar(2))
insert into [b]
select 2,'bh' union all
select 3,'bn' union all
select 4,'bm'

select b.kid, a.type from a, b where b.kid*=a.id  --带*
/*
The query uses non-ANSI outer join operators ("*=" or "=*"). 
*/
select b.kid, a.type from a, b where b.kid=a.id  --不带*
/*
kid         type
----------- ----
2           b
3           c
*/
select a.*,b.* from a left outer join b on b.kid=a.id --join
/*
id          type kid         col
----------- ---- ----------- ----
1           a    NULL        NULL
2           b    2           bh
3           c    3           bn
*/

------解决方案--------------------
1.第一种情况SQL SERVER 2005开始被取消。
2.第二种是内连接,第三种是左连接,结果不一样的。
------解决方案--------------------
第一种是老写发,2005 之后已经不支持


第二个是内链接

第三个是外连接