设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2202|回复: 6
打印 上一主题 下一主题

[查询] 关于VB+ACCESS+SQL联合查询的问题

[复制链接]
跳转到指定楼层
1#
发表于 2002-11-21 18:24:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我有一个表有如下的字段:
ncellid \ncell1\ncell2\ncell3\ncell4......ncell25共26个字段,现我想把符合条件的某行(既ncellid为某值的行 )的ncell1\ncell2\ncell3\ncell4......ncell25的字段中不为空的字段作成记录集同一字段的不同行.,名字为cellid.例如我的符合条件的表的行的数据如下:
ncellid  ncell1  ncell2  ncell3  ncell4......ncell25
20021     1        2       3       4           25
则查询的结果应为:

cellid   ncellid   
1`       20021
2        20021
3        20021
4        20021
..............
..............
25       20021

我用的方法如下有点笨,但目前想不出更好的来)
X$ = "select ncell1 as cellid ,ncellid   from neighber  where ncell1 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell2 ,ncellid   from neighber  where ncell2 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell3 ,ncellid   as nee  from neighber where ncell3 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell4 ,ncellid  from neighber where ncell4 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell5 ,ncellid  from neighber where ncell5 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell6 ,ncellid  from neighber where ncell6 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell7 ,ncellid  from neighber where ncell7 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell8 ,ncellid  from neighber where ncell8 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell9 ,ncellid  from neighber where ncell9 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell10 ,ncellid  from neighber where ncell10 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell11 ,ncellid  from neighber where ncell11 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell12 ,ncellid  from neighber where ncell12 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell13 ,ncellid  from neighber where ncell13 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell14 ,ncellid  from neighber where ncell14 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell15 ,ncellid  from neighber where ncell15 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell16 ,ncellid  from neighber where ncell16 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell17 ,ncellid  from neighber where ncell17 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell18 ,ncellid  from neighber where ncell18 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell19 ,ncellid  from neighber where ncell19 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell20 ,ncellid  from neighber where ncell20 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell21 ,ncellid  from neighber where ncell21 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell22 ,ncellid  from neighber where ncell22 <> 0 and ncellid = "
X$ = X$ & scellid
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
X$ = X$ & " union select ncell23 ,ncellid  from neighber where ncell23 <> 0 and ncellid = "
X$ = X$ & scellid

X$ = X$ & " union select ncell24 ,ncellid  from neighber where ncell24 <> 0 and ncellid = "
X$ = X$ & scellid


X$ = X$ & " union select ncell25 ,ncellid  from neighber where ncell25 <> 0 and ncellid = "
X$ = X$ & scellid
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

Set adjco = db.OpenRecordset(X$, dbOpenSnapshot)

当我去掉$$$之间的语句时程序正常,当加上后提示列别名错误。而我的后几条语句和前面的格式一样呀,是不是UNION查询最多允许22条语句呀?欢迎各位高手指点。如果不用这种方法还有没有别的简便方法?
[em27][em27][em27][em27][em27][em27][em27][em27]
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2002-11-21 23:26:00 | 只看该作者
是我没描述清楚吗?
3#
发表于 2002-11-21 23:49:00 | 只看该作者
能简明一点吗?有点晕
4#
 楼主| 发表于 2002-11-22 00:30:00 | 只看该作者
就是把某一行的数据做成一个表的列
5#
 楼主| 发表于 2002-11-22 17:13:00 | 只看该作者
斑竹帮忙呀,有什么高招吗?

点击这里给我发消息

6#
发表于 2002-11-22 21:30:00 | 只看该作者
交叉查询有这个功能。
7#
 楼主| 发表于 2002-11-23 03:43:00 | 只看该作者
没听过,可否举个例子
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-13 19:44 , Processed in 0.092014 second(s), 30 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表