Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 新列中作为csv字符串的多结果查询_Sql_Linq To Sql_Sql Server 2008 - Fatal编程技术网

Sql 新列中作为csv字符串的多结果查询

Sql 新列中作为csv字符串的多结果查询,sql,linq-to-sql,sql-server-2008,Sql,Linq To Sql,Sql Server 2008,我有三张桌子: 用户,产品用户2产品 每个产品都有ID 是否可以编写一个查询,结果我将得到两列: UserID, Products 在products列中,我将所有产品通过user2product表连接到user。Product表用逗号分隔。我们在谈论什么样的SQL方言? 对于postgresql,这里有一个非常全面的答案: 对于其他SQL系统,这个想法应该差不多相同。您必须搜索正确的聚合函数,其他所有操作都可以使用简单的groupby指令完成 干杯 Thilo很有可能在原生tsql中使用f

我有三张桌子:

用户,产品用户2产品

每个产品都有ID

是否可以编写一个查询,结果我将得到两列:

UserID, Products

在products列中,我将所有产品通过user2product表连接到user。Product表用逗号分隔。

我们在谈论什么样的SQL方言? 对于postgresql,这里有一个非常全面的答案:

对于其他SQL系统,这个想法应该差不多相同。您必须搜索正确的聚合函数,其他所有操作都可以使用简单的
groupby
指令完成

干杯
Thilo

很有可能在原生tsql中使用
for xml path
语句来实现这一点,详细说明如下

要通过linq在db上创建一个存储过程来访问它,请将它拖到dbml文件中,然后像方法一样从数据上下文调用它

sp的代码如下所示

select
    u.userid,
    substring(( select ',' + cast(p.productid as nvarchar(20))
        from
            user2product up inner join
            product p on up.productid = p.productid
        where
            up.userid = u.userid
        for xml path('')),2,2000000) as Products
from
    users u

您是想用T-SQL还是Linq来写这篇文章?刚才看到了SQL Server标记,因此它可能不会是Postgres。尽管如此,这个想法还是应该成立的。