看科學家研究,人會覺得走在左邊的人比較有男(女)人味,因此想泡妞的請走在女伴的左邊……………不是沒有道理的,台灣屬於靠右邊走的習慣,如果男士在女生左邊,會有保護女生讓女生走比較路邊比較安全的感覺............................再說連電腦SQL語法也是要你在左邊呢!!!!

 

昨天去書店剛好看到有介紹Join的書翻了一下,發現 left join 正符合我的需求,試了三個Table的 left Join一次把我要的都列表出來,省了另外兩個額外查詢,看文獻也說使用join比較有效率。

select g.*, cbas.custid, cbas.cmpabv, cntc.cntcid, cntc.phone from dbo.gdsinfomt g
left join dbo.custbas cbas on (g.Agent = cbas.custid)
left join dbo.custcntc cntc on ((g.Agent = cntc.custid) and (cntc.cntcid = substring(g.declno,1,2)))
where g.senddate >= '20100128'
and g.senddate <= '20100128'
and g.Gdsno like 'D%'

如果是土法鍊鋼的做法會有三個階段的查詢,會先查詢

select g.* from dbo.gdsinfomt g
where g.senddate >= '20100128'
and g.senddate <= '20100128'
and g.Gdsno like 'D%'

然後在 select g.* from dbo.gdsinfomt g 資料回圈內再一筆一筆依 g.Agent = cbas.custid 為條件查詢要的資料

select * from dbo.custbas cbas

where cbas.custid = g.Agent(上個查詢查到的資料)

第三個TABLE在 select g.* from dbo.gdsinfomt g 資料回圈內再一筆一筆依 g.Agent = cntc.custid and cntc.cntcid = substring(g.declno,1,2) 這兩個條件查詢

select * from dbo.custcntc cntc

where g.Agent = cntc.custid

and cntc.cntcid = substring(g.declno,1,2)

arrow
arrow
    全站熱搜

    NoMoney NoHoney 發表在 痞客邦 留言(0) 人氣()