left join 的怪问题

huxin1 2006-11-06 07:37:02
select com_1860_id,com_1860_proj_id,com_1860_yn_audit1,com_1860_yn_audit2,
com_1860_yn_audit3,com_1860_yn_resol,
com_1860_yn_recall,
com_1860_reason,com_1860_yn_withproj,
project_yn_finish,com_1860_item_class
from complaint_1860
left join project on( com_1860_id='A000001' and project_id=com_1860_proj_id )

上面这句SQL产生多条记录,记录的条数与complaint_1860 表中的数量一样,
而下面这句SQL却只产生一条记录,这让我很不解,这是为什么啊?两句就差了一个"left",说明一下,project_id和com_1860_id均为主键,
但com_1860_proj_id字段则不唯一


select com_1860_id,com_1860_proj_id,com_1860_yn_audit1,com_1860_yn_audit2,
com_1860_yn_audit3,com_1860_yn_resol,
com_1860_yn_recall,
com_1860_reason,com_1860_yn_withproj,
project_yn_finish,com_1860_item_class
from complaint_1860
join project on( com_1860_id='A000001' and project_id=com_1860_proj_id )
...全文
236 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
losedxyz 2006-11-06
  • 打赏
  • 举报
回复
join 是inner join的省略写法
fish_yht 2006-11-06
  • 打赏
  • 举报
回复
inner join 相当于 join 同样是内联
left join 是左外联用左表中的全部连接右表,并且显示左表中的全部数据
right join 是右外联,原理和左外联一样
out join 是外部连接,是不包括条件的两个表连接得到的结果
再具体看看联机帮助吧
上面说得很具体
xyxfly 2006-11-06
  • 打赏
  • 举报
回复
给你贴几个,其实自己把各种连接都在查询分析器里执行一下就知道了

declare @a table(a int,b int)
declare @b table(a int,b int)
insert @a values(1,1)
insert @a values(2,2)
insert @b values(1,1)
insert @b values(3,3)

--左:
select * from @a Aa left join @b Bb on Aa.a=Bb.a
--右:
select * from @a Aa right join @b Bb on Aa.a=Bb.a
--内
select * from @a Aa join @b Bb on Aa.a=Bb.a
--外:
select * from @a Aa full join @b Bb on Aa.a=Bb.a
--完全
select * from @a,@b


cross join 是笛卡儿乘积 就是一张表的行数乘以另一张表的行数
left join 第一张表的连接列在第二张表中没有匹配是,第二张表中的值返回null
right join 第二张表的连接列在第一张表中没有匹配是,第一张表中的值返回null
full join 返回两张表中的行 left join+right join
inner join 只返回两张表连接列的匹配项
xyxfly 2006-11-06
  • 打赏
  • 举报
回复
两句就差了一个"left"

呵呵,看看left join与join的区别
stou 2006-11-06
  • 打赏
  • 举报
回复
left join 會把左邊全顯示出來.
gc_ding 2006-11-06
  • 打赏
  • 举报
回复
因为join 是inner join的省略写法!
left join表示以complaint_1860 表为主,complaint_1860 表有的记录才显示出来
join(或inner join)表示complaint_1860 表与project都有的记录才显示出来

34,837

社区成员

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

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