@@@@@@@@@@@@@@@@@没多少分了 求一条多表查询语句@@@@@@@@@@@@@@@@

LWWL 2005-04-26 04:36:28
table1
日期 a b c
2004.01.01 a1 b1 c1
2004.01.01 a11 b11 c11
2004.02.01 a2 b2 c2

table2
日期 e f
2004.01.01 e1 f1
2004.03.01 e2 f2

table3
日期 g
2004.01.01 g1
2001.01.01 g11
2004.02.01 g2
2004.05.01 g3

我要得到这样一些数据
日期 a b c e f g
2001.01.01 a1 b1 c1 e1 f1 g1
2001.01.01 a11 b11 c11 g11
2001.02.01 a2 b2 c2 g2
2001.03.01 e2 f2
2001.05.01 g3
...全文
77 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
LWWL 2005-04-26
  • 打赏
  • 举报
回复
zjcxc(邹建) 接分 你太牛B了
LWWL 2005-04-26
  • 打赏
  • 举报
回复
我写错了 全部都是 2004
zjcxc 2005-04-26
  • 打赏
  • 举报
回复
select id=identity(int),* into #1 from table1
select id=identity(int),* into #2 from table2
select id=identity(int),* into #3 from table3

select 日期=isnull(a.日期,isnull(b.日期,c.日期)),
a=isnull(a.a,''),b=isnull(a.b,''),c=isnull(a.c,''),
e=isnull(b.e,''),f=isnull(e.f,''),
g=isnull(a.g,'')
from #1 a
full join #2 b on a.日期=b.日期
and(select count(*) from #1 where 日期=a.日期 and id<=a.id)
=(select count(*) from #2 where 日期=b.日期 and id<=b.id)
full join #3 c on a.日期=c.日期
and(select count(*) from #1 where 日期=a.日期 and id<=a.id)
=(select count(*) from #3 where 日期=c.日期 and id<=c.id)

drop table #1,#2,#3
xxljd 2005-04-26
  • 打赏
  • 举报
回复
......... 时间2004怎么变成2001了!
paoluo 2005-04-26
  • 打赏
  • 举报
回复
楼上的,你那样得不到楼主要的结果,测试看看。
LWWL 2005-04-26
  • 打赏
  • 举报
回复
不明白
xxljd 2005-04-26
  • 打赏
  • 举报
回复
只不过再加上一张table3 这张表
xxljd 2005-04-26
  • 打赏
  • 举报
回复
select *
from table1 inner join table2
on table1.日期=table2.日期 collate Chinese_PRC_CI_AI_WS



这样推下去 就出现你要得到的表了

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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