主从表横向关联的问题,比较麻烦,请进来看看
时间:2011-12-08
来源:互联网
现在有一个主表和n个从表
主表 y_list
id tea
1 001
2 002
3 004
4 003
5 101
从表表名分别为
y_list_001
y_list_002
y_list_004
y_list_003
y_list_101
从表的结构是一眼样的为
如:y_list_001
id pos userid out
1 张三 23 2
2 王五 24 7
3 随便 25 3
y_list_002
id pos userid out
1 张三 23 4
2 王五 24 2
3 随便 25 3
y_list_004
id pos userid out
1 张三 23 6
2 王五 24 2
3 随便 25 9
...
现在想把
select * from y_list的内容
和
select * from y_list_001 where userid=23 union all
select * from y_list_002 where userid=23 union all
select * from y_list_004 where userid=23 union all
select * from y_list_003 where userid=23 union all
select * from y_list_101 where userid=23
横向联合成这样的结果
tea pos userid out
001 张三 23 2
002 张三 23 4
004 张三 23 6
003 张三 23 8
101 张三 23 10
是在sqlserver 2k 下,注意是2000,不是2005
除了游标循环外怎么写最好呢,一定不要临时表,请高手帮忙谢谢,就这些分了,不够明天再加
主表 y_list
id tea
1 001
2 002
3 004
4 003
5 101
从表表名分别为
y_list_001
y_list_002
y_list_004
y_list_003
y_list_101
从表的结构是一眼样的为
如:y_list_001
id pos userid out
1 张三 23 2
2 王五 24 7
3 随便 25 3
y_list_002
id pos userid out
1 张三 23 4
2 王五 24 2
3 随便 25 3
y_list_004
id pos userid out
1 张三 23 6
2 王五 24 2
3 随便 25 9
...
现在想把
select * from y_list的内容
和
select * from y_list_001 where userid=23 union all
select * from y_list_002 where userid=23 union all
select * from y_list_004 where userid=23 union all
select * from y_list_003 where userid=23 union all
select * from y_list_101 where userid=23
横向联合成这样的结果
tea pos userid out
001 张三 23 2
002 张三 23 4
004 张三 23 6
003 张三 23 8
101 张三 23 10
是在sqlserver 2k 下,注意是2000,不是2005
除了游标循环外怎么写最好呢,一定不要临时表,请高手帮忙谢谢,就这些分了,不够明天再加
作者: aawwmate 发布时间: 2011-12-08
2000以前使过,现在没啥印象了。。。
作者: DenielJean 发布时间: 2011-12-08
SQL code
select * from 主表 a, (select *,1 as no from y_list_001 where userid=23 union all select *,2 from y_list_002 where userid=23 union all select *,3 from y_list_004 where userid=23 union all select *,4 from y_list_003 where userid=23 union all select *,5 from y_list_101 where userid=23) b where a.id=b.no
作者: ssp2009 发布时间: 2011-12-08
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28