SQL語句整理

暖楓無敵發表於2012-10-22

1、問題貼見(這裡一個注意點為:coalesce(欄位,0) 使用,即判斷這個欄位是否為NULL,如果為NULL,則為0)

http://topic.csdn.net/u/20121022/17/4b3a512f-e329-4b03-8aae-d4ce9c9c43f4.html?seed=1876494661&r=79962941#r_79962941


select tt.uname,tt.sumstart,ttt.laststart from
(
     select id,name as uname, COALESCE(SUM(start),0) as sumstart from [user] t1 left join team t2
     on t1.id=t2.uid  group by id,name
) tt,
(
     select t1.id,t1.name as uname, COALESCE(start,0)as laststart from [user] t1 left join team t2
     on t1.id=t2.uid and data=(select MAX(data) from team)
) ttt
where tt.id=ttt.id;




級聯更新表欄位

update ST_PPTN_D  set LGTD= a.LGTD,LTTD=a.LTTD 
from ST_STBPRP_B a  inner join ST_PPTN_D p on p.STCD=a.STCD;