Office中国论坛/Access中国论坛

标题: 求助:这个查询代码如何写? [打印本页]

作者: yhf    时间: 2012-6-16 11:12
标题: 求助:这个查询代码如何写?
附件db.mdb中有2个表:学校、表1。 请教诸位:现要在窗体1中点击 更新 按钮后,根据右侧子窗体内容 生成左侧列表 内容。谢谢!(注:右侧子窗体中记录数是不确定的,但至少2条)
作者: Henry D. Sy    时间: 2012-6-16 11:52
描述不清,表1应该是不存在的,只是你想要的结果对吗?
作者: anthonywang9    时间: 2012-6-16 12:38
本帖最后由 anthonywang9 于 2012-6-16 12:40 编辑

试试这个
SELECT *
FROM
(SELECT ID as 组合ID,学校 as 组合学校,代码 as 组合代码
from 学校
union
SELECT [a.ID]&[b.ID] AS 组合ID, [a.学校]&";"&[a.代码]&";"&[b.学校]&";"&[b.代码] AS 组合学校, [a.代码]&"/"&[b.代码] AS 组合代码
FROM 学校 AS a, 学校 AS b
WHERE a.ID<b.ID
union
SELECT [a.ID]&[b.ID]&[c.ID] AS 组合ID, [a.学校]&";"&[a.代码]&";"&[b.学校]&";"&[b.代码]&";"&[c.学校]&";"&[c.代码] AS 组合学校, [a.代码]&"/"&[b.代码]&"/"&[c.代码] AS 组合代码
FROM 学校 AS a, 学校 AS b, 学校 AS c
WHERE a.ID<b.ID and b.id<c.ID
union
SELECT [a.ID]&[b.ID]&[c.ID]&[d.ID] AS 组合ID, [a.学校]&";"&[a.代码]&";"&[b.学校]&";"&[b.代码]&";"&[c.学校]&";"&[c.代码]&";"&[d.学校]&";"&[d.代码] AS 组合学校, [a.代码]&"/"&[b.代码]&"/"&[c.代码]&"/"&[c.代码] AS 组合代码
FROM 学校 AS a, 学校 AS b, 学校 AS c, 学校 AS d
WHERE a.ID<b.ID and b.id<c.ID and c.id<d.ID)  AS [%$##@_Alias]
ORDER BY cint([组合ID]);

作者: yhf    时间: 2012-6-16 13:05
Henry D. Sy 发表于 2012-6-16 11:52
描述不清,表1应该是不存在的,只是你想要的结果对吗?

表1存在,更新后的数据保存在表1.谢谢!
作者: yhf    时间: 2012-6-16 13:11
anthonywang9 发表于 2012-6-16 12:38
试试这个
SELECT *
FROM

太棒了,谢谢!
作者: yhf    时间: 2012-6-16 13:46
anthonywang9 发表于 2012-6-16 12:38
试试这个
SELECT *
FROM

谢谢anthonywang9 :我试了一下,可用。但只适用最多4条记录,学校表内记录数大于4时,查询生成的数据不全(比如5条记录时,组合ID就缺少12345,6条记录时出错:溢出)
作者: anthonywang9    时间: 2012-6-16 15:14
额,我以为你的学校校区“东南西北”完了啊~~
如果你的源表【学校】记录数(r)是可变的话,那么就存在r个表间自连接,这种情况的话最好用VBA写吧,单纯的SQL语句可能不能写出实现这样的功能。
作者: yhf    时间: 2012-6-16 21:14
anthonywang9 发表于 2012-6-16 15:14
额,我以为你的学校校区“东南西北”完了啊~~
如果你的源表【学校】记录数(r)是可变的话,那么就存在r个 ...

请教 anthonywang9: VBA该如何写? 谢谢!
作者: Henry D. Sy    时间: 2012-6-17 09:35
好像没有规律,如果没有规律那只能手工输入记录了。{:soso_e110:}
看了半天也看不出具体规律。
比如看不到134组合!
作者: anthonywang9    时间: 2012-6-17 21:53
本帖最后由 anthonywang9 于 2012-6-17 21:57 编辑

Sub a()
Dim i As Integer: Dim m%
Dim Tsql As String: Dim sql() As String
i = DCount("ID", "学校")
ReDim sql(1 To i)
For m = 1 To i
sql(m) = "select " + QuerySub1(m) + "," + QuerySub2(m) + "," + QuerySub3(m) + QuerySub4(m) + QuerySub5(m)
Tsql = Tsql + " union " + sql(m)
Next m
Tsql = "selcet * from (" + Right(Tsql, Len(Tsql) - 6) + ") order by cint([组合ID])"
End Sub

Function QuerySub1(count As Integer) As String
Dim i%
For i = 1 To count
QuerySub1 = QuerySub1 + "&[a" & i & ".ID]"
Next i
QuerySub1 = Right(QuerySub1, Len(QuerySub1) - 1) + " as 组合ID"
End Function


Function QuerySub2(count As Integer) As String
Dim i%
For i = 1 To count
QuerySub2 = QuerySub2 + "&"";""&[a" & i & ".学校]&"";""&[a" & i & ".代码]"
Next i
QuerySub2 = Right(QuerySub2, Len(QuerySub2) - 5) + " as 组合学校"
End Function


Function QuerySub3(count As Integer) As String
Dim i%
For i = 1 To count
QuerySub3 = QuerySub3 + "&""/""&[a" & i & ".代码]"
Next i
QuerySub3 = Right(QuerySub3, Len(QuerySub3) - 5) + " as 组合代码"
End Function


Function QuerySub4(count As Integer) As String
Dim i%
For i = 1 To count
QuerySub4 = QuerySub4 + ",学校 as a" & i
Next i
QuerySub4 = " from " + Right(QuerySub4, Len(QuerySub4) - 1)
End Function


Function QuerySub5(count As Integer) As String
Dim i%
If count = 1 Then
QuerySub5 = ""
Else
For i = 1 To count - 1
QuerySub5 = QuerySub5 + " and a" & i & ".ID<a" & (i + 1) & ".ID"
Next i
QuerySub5 = " where " + Right(QuerySub5, Len(QuerySub5) - 4)
End If
End Function

Tsql是最终的查询语句,完全的sql语句拼接策略。由于Tsql长度过长,我的access无法支持调试。其中对于每一个m值的sql(m)我已经调试过,没有问题。
你可以建立i = DCount("ID", "学校")个中间查询,其中QueryDef(i)=sql(i),然后再使用union方法。
作者: yhf    时间: 2012-6-19 22:14
anthonywang9 发表于 2012-6-17 21:53
Sub a()
Dim i As Integer: Dim m%
Dim Tsql As String: Dim sql() As String

收下了,非常感谢!




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3