这种情况如何查询好些?

dtmyou 2003-08-23 12:13:44
table1
id column11 column12
1 4 2
2 5 3
table2
id column21 column22
1 1 2
2 2 3
3 3 5
4 4 7
5 5 8
table3
id column31 column32
1 1 4
2 2 9
3 3 0
其中table2中的column21唯一,且对应于table1中的column11或table3中的column31
要求得到结果:
id table2id column21 column22 TmpColumn
1 1 1 2 4
2 2 2 3 9
3 3 3 5 0
4 4 4 7 2
5 5 5 8 3
其中id为从1开始的自增字段

PS:我目前解决方法是在table2中增加一个字段btable用来标识该记录在table3或是table1中,1表示在table3中,0表示在table1中
现table2情况如下
id column21 column22 btable
1 1 2 1
2 2 3 1
3 3 5 1
4 4 7 0
5 5 8 0

select table2.id as table2id,column21,column22,
tmpColumn = case when btable = 1 then (select column32 from table3 where column31 = table2.column1) else (select column12 from table1 where column11 = table2.column1) end from table2
(id还没想到怎么去解决)

有没有好些的办法...

多谢,不胜感激!!
...全文
36 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yijiayi 2003-08-23
  • 打赏
  • 举报
回复

select a.*,isnull(b.column12,c.column32)
from table2 a full join table1 b on a.column21=b.column11
full join table3 c on a.column21=c.column31

happydreamer 2003-08-23
  • 打赏
  • 举报
回复

select a.*,isnull(b.column12,c.column32)
from table2 a full join table1 b on a.column21=b.column11
full join table3 c on a.column21=c.column31
hjb111 2003-08-23
  • 打赏
  • 举报
回复
select * into #t from table3
insert into #t(column31,column32) select column11,column12 from table2
select a.*,b.column32 btable from table2 a,#t b where a.id=b.id
pengdali 2003-08-23
  • 打赏
  • 举报
回复
测试:

declare @table1 table(id int identity(1,1),column11 int,column12 int)
insert into @table1
select 4,2
union all select 5,3

declare @table2 table(id int identity(1,1),column21 int,column22 int)
insert into @table2
select 1,2
union all select 2,3
union all select 3,5
union all select 4,7
union all select 5,8

declare @table3 table(id int identity(1,1),column31 int,column32 int)
insert into @table3
select 1,4
union all select 2,9
union all select 3,0


select (select sum(1) from @table2 where id<=a.id) [id],a.*,b.column12 from @table2 a left join (select column11,column12 from @table1 union all select column31,column32 from @table3) b on a.column21=b.column11

pengdali 2003-08-23
  • 打赏
  • 举报
回复
select (select sum(1) from table2 where id<=a.id) [id],a.*,b.column12 from table2 a left join (select column11,column12 from table1 union all select column31,column32 from table3) b on a.column21=b.column11

就可以了。
pengdali 2003-08-23
  • 打赏
  • 举报
回复
select (select sum(1) from table2 where id<=a.id) [id],a.*,b.column12 from table2 a left join (select column11,column12 from @table1 union all select column31,column32 from table3) b on a.column21=b.column11

就可以了。
accounant 2003-08-23
  • 打赏
  • 举报
回复

select identity(int,1,1) as id,a.id as table2id,column21,column22,isnull(b.column12,c.column32) as column32
into #temp
from
table2 a full join table1 b on a.column21=b.column11
full join table3 c on a.column21=c.column31
select * from #temp

drop table #temp
zjcxc 元老 2003-08-23
  • 打赏
  • 举报
回复
下面是数据测试

--创建数据测试环境
declare @table1 table(id int identity(1,1),column11 int,column12 int)
insert into @table1
select 4,2
union all select 5,3

declare @table2 table(id int identity(1,1),column21 int,column22 int)
insert into @table2
select 1,2
union all select 2,3
union all select 3,5
union all select 4,7
union all select 5,8

declare @table3 table(id int identity(1,1),column31 int,column32 int)
insert into @table3
select 1,4
union all select 2,9
union all select 3,0

--查询结果
select id,table2id,column21,column22,TmpColumn=isnull(column12,column32)
from(
select id=(select count(*) from @table2 where id<=a.id),
table2id=a.id,a.column21,a.column22,b.column12,c.column32
from @table2 a
left join @table1 b on a.column21=b.column11
left join @table3 c on a.column21=c.column31
) a order by table2id




--/*--语句的执行结果
id table2id column21 column22 TmpColumn
----------- ----------- ----------- ----------- -----------
1 1 1 2 4
2 2 2 3 9
3 3 3 5 0
4 4 4 7 2
5 5 5 8 3

(所影响的行数为 5 行)
-------------------------------------------------------------------*/
zjcxc 元老 2003-08-23
  • 打赏
  • 举报
回复
select id,table2id,column21,column22,TmpColumn=isnull(column12,column32)
from(
select id=(select count(*) from table2 where id<=a.id),
table2id=a.id,a.column21,a.column22,b.column12,c.column32
from table2 a
left join table1 b on a.column21=b.column11
left join table3 c on a.column21=c.column31
) a order by table2id
zjcxc 元老 2003-08-23
  • 打赏
  • 举报
回复
select id,table2id,column21,column22,TmpColumn=isnull(column12,column32)
from(
select id=(select count(*) from @table2 where id<=a.id),
table2id=a.id,a.column21,a.column22,b.column12,c.column32
from @table2 a
left join @table1 b on a.column21=b.column11
left join @table3 c on a.column21=c.column31
) a order by table2id
pengdali 2003-08-23
  • 打赏
  • 举报
回复
select (select sum(1) from table2 where id<=a.id) [id],a.*,b.column11 from table2 a left join (select column11,column12 from table1 union all select column31,column32 from table3) b on a.column21=b.column11
nboys 2003-08-23
  • 打赏
  • 举报
回复
or

select identity(int,1,1) as id,a.id as table2id,column12,column22,isnull(b.column12,c.column32) as column32
into #temp
from
table2 a full join table1 b on a.column21=b.column11
full join table3 c on a.column21=c.column31
select * from #temp

drop table #temp

nboys 2003-08-23
  • 打赏
  • 举报
回复
select indentity(int,1,1) as id,b.id as table2id,column12,column22,column32 as TmpColumn
into #temp
from
table2 a left join table3 b on a.column21=b.column31
left join table1 c on a.column21=c.column11


select * from #temp

drop table #temp
txlicenhe 2003-08-23
  • 打赏
  • 举报
回复
select a.*,isnull(b.column12,c.column32)
from table2 a
left join table1 b on a.column21=b.column11
left join table3 c on a.column21=c.column31


or:

Select identity(int,1,1) as id,a.*,isnull(b.column12,c.column32)
into #tmp
from table2 a
left join table1 b on a.column21=b.column11
left join table3 c on a.column21=c.column31

Select * from #tmp

drop table #tmp


34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧