我如何显示这样的数据 [英] HOW DO I SHOW ROW DATA LIKE THIS

查看:74
本文介绍了我如何显示这样的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个包含以下数据的SQL表,



Col1(ID),Col2(名称)

1-- ------- 1

1 --------- 2

1 --------- 3

2 --------- 1

2 --------- 2

2 ------- - 3

3 --------- 1



所以我需要做的是将它们绑定到表格会是这样的,



ID,名称

1 --- 1,2,3

2 --- 1,2,3

3 --- 1



它也应该是数据绑定。



因此,每当我添加具有类似ID的新数据时,刷新时都会出现一个新名称。

Basically I have an SQL table with the following data,

Col1(ID), Col2(name)
1---------1
1---------2
1---------3
2---------1
2---------2
2---------3
3---------1

So what I need to do is to bind them to a table where it would be like so,

ID , Name
1 --- 1,2,3
2 --- 1,2,3
3 --- 1

And it should be data binded too.

So whenever I add new data that has the similar ID then a new name should appear when refreshed.

推荐答案

创建一个函数

Create a function
CREATE FUNCTION dbo.ConcatOrder(@ParentId int,@ChildId int)
 RETURNS VARCHAR(8000)
 AS
 BEGIN
     DECLARE @Output VARCHAR(8000)
     SELECT @Output = COALESCE(@Output+', ', '') + CONVERT(varchar(20), PatientID)
     FROM <tabname> ----- please change tablename here
     ORDER BY ParentId ,ChildId 
     RETURN @Output
 END
 GO
</tabname>



使用该功能


Use that function

 SELECT Col1, dbo.ConcatOrder(Col1, Col2)
 FROM <tablename> ----- please change tablename here
</tablename>


这篇关于我如何显示这样的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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