在数据库(MS接入),或在服务器端code(ASP.NET)串联记录值 [英] Concatenating record values in database (MS Access) or in server side code (ASP.NET)

查看:122
本文介绍了在数据库(MS接入),或在服务器端code(ASP.NET)串联记录值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不好意思再打扰你的人。我已经搜查所有在互联网上,但我不能找到解决我的问题。我在访问两个表和输出是这样的:


MATH 5
ENGLISH 3
ENGLISH 2
物理5
MATH 1
MATH 3

我希望它是:


MATH 5,1,3
ENGLISH 3,2
物理5

我怎样才能做到这一点?它试图在Access数据库与SQL命令打但没有任何工程。我也看了使用GridView控件在ASP.NET中,但没有运气任何一种解决方案。有没有办法来acomplish这还是我应该采取不同的方式?

最好的我所能做的就是GROUP BY所以输出如下:


MATH 5
MATH 3
MATH 1
物理3
物理1
...


解决方案

有一个简单的解决方案。使用最后一个()聚合函数查询。

示例:

  SELECT RECNO,最后(fConcat([RECNO],[字段5]))AS字段5 FROM mytable的GROUP BY RECNO;

函数尾()调用将被分组每条记录功能的毗连。请注意若要使用该函数的毗连之外定义的全局变量。

下面是一个简单的code作为一个概念证明:

 选项比较数据库昏暗glbWert1为Variant,glbWert2为Variant公共职能fConcat(strWer​​t1,strWer​​t2)为Variant  如果strWer​​t1<> glbWert1然后
    glbWert1 = strWer​​t1
    glbWert2 = strWer​​t2
  其他
    glbWert2 = glbWert2 +;+ strWer​​t2
  万一  fConcat = glbWert2结束功能

解决方案的工作非常快。

sorry to bother you people again. I've searched all over the internet but I can't find the solution to my problem. I have two tables in Access and the output is like this:

MATH 5
ENGLISH 3
ENGLISH 2
PHYSICS 5
MATH 1
MATH 3

I want it to be:

MATH 5, 1, 3
ENGLISH 3, 2
PHYSICS 5

How can I accomplish this? It tried playing with SQL commands in Access database but nothing works. I also looked for solution using the GridView in ASP.NET but no luck either. Is there a way to acomplish this or should I take a different approach?

Best I can do is GROUP BY so the output looks like this:

MATH 5
MATH 3
MATH 1
PHYSICS 3
PHYSICS 1
...

解决方案

There is an easy solution. Use the last() aggregate function in your query.

Sample:

SELECT RecNo, Last(fConcat([RecNo],[Field5])) AS Field5 FROM myTable GROUP BY RecNo; 

The function Last() calls the function Concat for each record that will be grouped. Be aware to use global variables that are defined outside the function Concat.

The following is just a simple code as a proof of concept:

Option Compare Database

Dim glbWert1 As Variant, glbWert2 As Variant

Public Function fConcat(strWert1, strWert2) As Variant

  If strWert1 <> glbWert1 Then
    glbWert1 = strWert1
    glbWert2 = strWert2
  Else
    glbWert2 = glbWert2 + "; " + strWert2
  End If

  fConcat= glbWert2

End Function

The solution works very fast

这篇关于在数据库(MS接入),或在服务器端code(ASP.NET)串联记录值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆