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

left join 怎么使用

2013-08-09 
left join 如何使用?select * from schedulingShipSchedule where Sub_ShipDate1and Sub_Qty1 and

left join 如何使用?


  select * from schedulingShipSchedule where Sub_ShipDate1<>''and Sub_Qty1<>'' and TeamHandlingBy='Non-Mailing'
  

以上代碼可以得到55條記錄


  with a as 
  (select * from schedulingShipSchedule where Sub_ShipDate1<>''and Sub_Qty1<>'' and TeamHandlingBy='Non-Mailing'),
  b as 
  (select * from DirectOrderV2soitems where invoiceornot='2' or invoiceornot='3')
  select  a.JobNumber,a.Version,b.HonourJob,b.itemnumber  
  from a left join b on 
  a.Version=b.itemnumber and  a.JobNumber=b.HonourJob 


為什麼使用左連接之後出現63條記錄,為什麼不是55條記錄?

[解决办法]
举例说明什么是一对多..

with a as
( select 1 'id', 'a1' 'x' union all
  select 2 'id', 'a2' 'x' union all
  select 3 'id', 'a3' 'x'
),
b as
( select 1 'id', 'b1' 'y' union all
  select 2 'id', 'b21' 'y' union all
  select 2 'id', 'b22' 'y' union all
  select 2 'id', 'b23' 'y' union all
  select 3 'id', 'b3' 'y'
)
select * 
 from a
 left join b on a.id=b.id


-- 结果, a表数据3条,左连接之后出现5条记录.因为a.id=2的记录对应b.id=2有3条记录.
/*
id          x    id          y
----------- ---- ----------- ----
1           a1   1           b1
2           a2   2           b21
2           a2   2           b22


2           a2   2           b23
3           a3   3           b3

(5 row(s) affected)
*/

热点排行