左外关联的语法比较。

svcce 2012-07-28 02:14:42

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


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

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

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

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

{第三种是常用的join,一般都这么进行做外关联查询。}
...全文
253 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
svcce 2012-07-28
  • 打赏
  • 举报
回复
谢谢解答!
筱筱澄 2012-07-28
  • 打赏
  • 举报
回复
第一种是老写发,2005 之后已经不支持


第二个是内链接

第三个是外连接
叶子 2012-07-28
  • 打赏
  • 举报
回复
1.第一种情况SQL SERVER 2005开始被取消。
2.第二种是内连接,第三种是左连接,结果不一样的。
叶子 2012-07-28
  • 打赏
  • 举报
回复

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
*/
Mr_Nice 2012-07-28
  • 打赏
  • 举报
回复
1、为了兼容之前的语法. *= 这个是在2000之前广泛应用的
2、? 没有碰到不能正确显示的问题。
3、这还得取决于LZ的需求,如果一定要相等id的数据,那么inner join 会是第一选择。


34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧