create table sl1(cwhcode varchar(10),cwh_name varchar(10),sl_1 int)
insert sl1 values('10','联盟',1)
insert sl1 values('06','龙家',2)
insert sl1 values('03','新法',1)
create table sl2(cwhcode varchar(10),cwh_name varchar(10),sl_2 int)
insert sl2 values('01','田坝',1)
insert sl2 values('05','土木',1)create table sl3(cwhcode varchar(10),cwh_name varchar(10),sl_3 int)
insert sl3 values('01','田坝',1)goselect a.*,[sl_1]=isnull(b.sl_1,0),[sl_2]=isnull(c.sl_2,0),[sl_3]=isnull(d.sl_3,0) from
(
select cwhcode,cwh_name from sl1
union
select cwhcode,cwh_name from sl2
union
select cwhcode,cwh_name from sl3
) a left join sl1 b on a.cwhcode=b.cwhcode
left join sl2 c on a.cwhcode=c.cwhcode
left join sl3 d on a.cwhcode=d.cwhcode
insert sl1 values('10','联盟',1)
insert sl1 values('06','龙家',2)
insert sl1 values('03','新法',1)
create table sl2(cwhcode varchar(10),cwh_name varchar(10),sl_2 int)
insert sl2 values('01','田坝',1)
insert sl2 values('05','土木',1)create table sl3(cwhcode varchar(10),cwh_name varchar(10),sl_3 int)
insert sl3 values('01','田坝',1)goselect a.*,[sl_1]=isnull(b.sl_1,0),[sl_2]=isnull(c.sl_2,0),[sl_3]=isnull(d.sl_3,0) from
(
select cwhcode,cwh_name from sl1
union
select cwhcode,cwh_name from sl2
union
select cwhcode,cwh_name from sl3
) a left join sl1 b on a.cwhcode=b.cwhcode
left join sl2 c on a.cwhcode=c.cwhcode
left join sl3 d on a.cwhcode=d.cwhcode
解决方案 »
- 一个查询语句?
- 写个存储过程,如何遍历所有行,并根据条件设置行?
- jbuilder 中提示游标未声明。。。。
- MySQL数据库 select不到内容
- 一个ACCESS数据库同时两个程序要调用,当第一个程序调用这个数据库后,第二个程序打开时就提示该数据库正在使用中,不能打开,如何办?
- zjcxc(邹建)请进,请教一个触角器问题!!急急
- 不知道是不是锁的原因?还是索引有问题?数据不能修改,请解答。
- sql查询的问题,帮我看看吧,在线急等
- select * from a in 的问题
- 如何得到指定数目的记录集??
- 小问题
- 我想用EXEC语句重新写这条INSERT INTO ...... SELECT语句???
insert #sl_1 values('10','联盟',1)
insert #sl_1 values('06','龙家',2)
insert #sl_1 values('03','新法',1)
create table #sl_2(cwhcode varchar(10),cwh_name varchar(10),sl_2 int)
insert #sl_2 values('01','田坝',1)
insert #sl_2 values('05','土木',1)create table #sl_3(cwhcode varchar(10),cwh_name varchar(10),sl_3 int)
insert #sl_3 values('01','田坝',1)
select pub.cwhcode,
pub.cwh_name,
isnull(a.sl_1,0),
isnull(b.sl_2,0),
isnull(c.sl_3,0)
from (select a.cwhcode,a.cwh_name from #sl_1 a union select b.cwhcode ,b.cwh_name from #sl_2 b union select c.cwhcode,c.cwh_name from #sl_3 c ) pub
left join #sl_1 a on pub.cwhcode=a.cwhcode
left join #sl_2 b on pub.cwhcode=b.cwhcode
left join #sl_3 c on pub.cwhcode=c.cwhcode
order by pub.cwhcode descdrop table #sl_1
drop table #sl_2
drop table #sl_3
结果-----
10 联盟 1 0 0
06 龙家 2 0 0
05 土木 0 1 0
03 新法 1 0 0
01 田坝 0 1 1
drop table sl_1
go
create table sl_1(cwhcode varchar(10),cwh_name varchar(10),sl_1 int)
insert into sl_1(cwhcode,cwh_name,sl_1) values('10', '联盟', 1)
insert into sl_1(cwhcode,cwh_name,sl_1) values('06', '龙家', 2)
insert into sl_1(cwhcode,cwh_name,sl_1) values('03', '新法', 1)
goif object_id('pubs..sl_2') is not null
drop table sl_2
go
create table sl_2(cwhcode varchar(10),cwh_name varchar(10),sl_2 int)
insert into sl_2(cwhcode,cwh_name,sl_2) values('01', '田坝', 1)
insert into sl_2(cwhcode,cwh_name,sl_2) values('05', '土木', 1)
goif object_id('pubs..sl_3') is not null
drop table sl_3
go
create table sl_3(cwhcode varchar(10),cwh_name varchar(10),sl_3 int)
insert into sl_3(cwhcode,cwh_name,sl_3) values('01', '田坝', 1)
go
select cwhcode,cwh_name,max(sl_1) sl_1 , max(sl_2) sl_2 , max(sl_3) sl_3 from
(
select cwhcode,cwh_name,sl_1 , sl_2 = 0 , sl_3 = 0 from sl_1
union all
select cwhcode,cwh_name,sl_1 = 0 , sl_2 , sl_3 = 0 from sl_2
union all
select cwhcode,cwh_name,sl_1 = 0 , sl_2 = 0 , sl_3 from sl_3
) t
group by cwhcode,cwh_name
order by cwhcode,cwh_namedrop table sl_1,sl_2,sl_3/*
cwhcode cwh_name sl_1 sl_2 sl_3
---------- ---------- ----------- ----------- -----------
01 田坝 0 1 1
03 新法 1 0 0
05 土木 0 1 0
06 龙家 2 0 0
10 联盟 1 0 0(所影响的行数为 5 行)
*/
union select cwhcode ,cwh_name from sl2
union select cwhcode ,cwh_name from sl3 )
select #t.cwhcode,#t.cwh_name,isnull(sl_1,0)sl_1,isnull(sl_2,0) sl_2,isnull(sl_3,0) sl_3 from
#t left join sl1 on #t.cwhcode=sl1.cwhcode left join sl2 on #t.cwhcode=sl2.cwhcode
left join sl3 on #t.cwhcode=sl3.cwhcode
drop table #t
(
select cwhcode,cwh_name from sl1
union
select cwhcode,cwh_name from sl2
union
select cwhcode,cwh_name from sl3
) a
left join sl1 b on a.cwhcode=b.cwhcode
left join sl2 c on a.cwhcode=c.cwhcode
left join sl3 d on a.cwhcode=d.cwhcode
union
select cwhcode,cwh_name,sl_1=0,sl_2,sl_3=0 from sl2
union
select cwhcode,cwh_name,sl_1=0,sl_2=0,sl_3 from sl3
union
select cwhcode,cwh_name,sl_1=0,sl_2,sl_3=0 from #sl2
union
select cwhcode,cwh_name,sl_1=0,sl_2=0,sl_3 from #sl3