检索SQL关系作为逗号分隔的字符串 [英] Retrieving SQL Relationships as a comma delimited string

查看:410
本文介绍了检索SQL关系作为逗号分隔的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL Server 2005数据库有两个表:Order,LineItem。每个LineItem都有一个名为LineItemID和OrderID的字段。我有一个查询,获取所有的数据库中的订单记录。对于每个订单记录,我想检索与订单相关联的LineItemID的逗号分隔列表。

I have a SQL Server 2005 database with two tables: Order, LineItem. Each LineItem has a field called LineItemID and OrderID. I have a query that is getting all of the Order records in my database. With each Order record, I would like to retrieve a comma delimited list of LineItemIDs associated with the Order.

在SQL中有办法做到这一点吗?

Is there a way to do this in SQL? I do not know how to do this.

谢谢!

推荐答案

p>这里有一个例子,使用sys.tables中的name列,如何从列中构造以逗号分隔的字符串:

Here's one example, using the name column from sys.tables, of how to construct a comma-delimited string from a column:

use master
go

SELECT Stuff((SELECT ',' + name
              FROM sys.tables
              For XML PATH ('')),1,1,'')
go

这篇关于检索SQL关系作为逗号分隔的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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