问个sql语句,三表合并(100)

  • 主题发起人 主题发起人 wlyft
  • 开始时间 开始时间
W

wlyft

Unregistered / Unconfirmed
GUEST, unregistred user!
表1 ID name kcsl1 x 12 c 24 d 1表2 ID name cgsl1 s 12 c 13 q 1表3 ID name sqsl2 c 13 q 14 d 1生成 ID name kcsl cgsl sqsl 1 x 1 0 01 s 0 1 02 c 2 1 13 q 0 1 14 d 1 0 1这语句怎么写
 
select id,name,sum(kcsl) as kcsl,sum(cgsl) as cgsl,sum(sgsl) as sgsl from(select ID,name,kcsl,0 as cgsl,0 as sgsl from tb1 union select id,name,0 as kcsl,cgsl,0 as sgsl from tb2 union select id,name,0 as kcsl,0 as cgsl,sgsl from tb3) as tbgroup by id,nameorder by id,name desc
 
select id,name,sum(kcsl) as kcs,sum(cgsl),sum(sqsl) from (select id,name,kcsl,0 as cgsl,0 as sqsl from 表1union allselect idid,name,0 as kcsl,cgsl,0 as sqsl from 表2union allselect id,name,0 as kcsl,0 as cgsl,sqsl from 表3) xgroup by id,name;
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
639
import
I
I
回复
0
查看
850
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部