[英]is there a group_concat function in ms-access?
在ms-access或類似的東西中是否有group_concat函數?
你應該問問自己是否需要一個通用的解決方案( 另一個是Allen Browne ),或者你是否需要一個通用的解決方案。 如果你真的只需要這一次,那么就這么簡單。
在旁注中,當在VBA代碼中連接列表時,利用長期訪問大師Trevor Best向我講授的技巧,那就是在每個值的開頭粘貼分隔符,然后使用Mid()來剝離它關閉。 而不是通過子記錄循環內部:
If Len(strOutput) = 0 Then
strOutput = NewValue
Else
strOutput = strOutput & ", " & NewValue
End If
...在循環中使用它:
strOutput = strOutput & ", " & NewValue
...然后當你退出循環時,剝去前導分隔符:
strOutput = Mid(strOutput, 3)
這在整個地方都有影響,並簡化了在大量上下文中連接的代碼。
我發現Duane Hookum(微軟MVP)的這篇帖子聲稱能夠做你想做的事。 我沒有測試過它。
順便說一句,如果你感興趣,這就是我發現它的方式:
第一次搜索: group_concat訪問引導我到這個帖子有這個答案,但鏈接被破壞了 。
然后我在答案試圖鏈接的內容之后再次搜索,並找到它: site:http://www.rogersaccesslibrary.com/ concatenate 。
不可以.Access沒有GROUP_CONCAT功能。 但是,可以創建一個VBA函數,它允許您傳遞包含SQL語句的字符串並獲得等效功能(不是我推薦它,但它是可能的)。
以我自己的個人回程機器為例,這是我在恐龍統治地球時回寫的一些代碼:
Public Function ListQuery(SQL As String _
, Optional ColumnDelimiter As String = " " _
, Optional RowDelimter As String = vbCrLf) As String
'PURPOSE: to return a combined string from the passed query
'ARGS:
' 1. SQL is a valid Select statement
' 2. ColumnDelimiter is the character(s) that separate each column
' 3. RowDelimiter is the character(s) that separate each row
'RETURN VAL:
'DESIGN NOTES:
Const PROCNAME = "ListQuery"
Const MAXROWS = 100
Const MAXCOLS = 10
Dim oConn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim oField As ADODB.Field
Dim sRow As cString
Dim sResult As cString
On Error GoTo ProcErr
Set sResult = New cString
Set sRow = New cString
Set oConn = GetADOConn()
sResult.Clear
Do Until oRS.EOF
sRow.Clear
For Each oField In oRS.Fields
With sRow
If .Length > 0 Then
.Append ColumnDelimiter
End If
.Append Nz(oField.Value)
End With
Next oField
sRow.Trim
If sRow.Length > 0 Then
With sResult
.Append sRow
.Append RowDelimter
End With
End If
oRS.MoveNext
Loop
oRS.Close
oConn.Close
With sResult
If .Right(Len(RowDelimter)).Value = RowDelimter Then
.Length = .Length - Len(RowDelimter)
End If
End With
FunctionResult:
ListQuery = sResult.Value
CleanUp:
Set sResult = Nothing
Set sRow = Nothing
Set oField = Nothing
Set oRS = Nothing
Set oConn = Nothing
Exit Function
ProcErr:
' logging code...
Resume CleanUp
End Function
GetADOConn
函數是一個集中函數,用於檢索當前數據庫連接。 cString
是一個模仿.NET的StringBuilder
類行為的類,但是在.NET不是TLD和營銷炒作之前很久就編寫過了。 由於在每一行都會調用它,因此VBA的內置字符串連接速度很慢,因此需要類似StringBuilder
類的東西。 原始代碼(我已經部分修改過)對可以使用的行數和列數有一個上限,這是常量的全部內容。
有一個訪問函數可以將多個值組合成一個值(我猜是自定義聚合。)鏈接是http://www.rogersaccesslibrary.com/Otherdownload.asp?SampleName='Generic%20Function%20To%20Concatenate%20Child% 20條
但該網站現在已經停止。 如果你谷歌href,你會發現很多參考和例子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.