【问题标题】:multiple row value seperated by comma in sql databasesql数据库中用逗号分隔的多行值
【发布时间】:2016-06-21 16:19:18
【问题描述】:

我有一个电子商务网站,一个 ID 可以有多个产品订单。截至目前,我有这个:

如您所见,它在我的桌子上显示了冗余。我想要这样的输出:

Carbon Dixode, Industrial Oxygen

因为它们在同一个 ID (10) 上。也一样

Compressed Air, Kerosene, Medical Oxygen

这是我的代码:

private void GetOrderList()
    {
        ShoppingCart k = new ShoppingCart()
        {
            Flag = 0
        };
        DataTable dt = k.GetOrderList();

        gvCustomerOrders.DataSource = dt;
        gvCustomerOrders.DataBind();
        gvCustomerOrders.HeaderRow.TableSection = TableRowSection.TableHeader;
    }

这是 GetOrderList()

internal DataTable GetOrderList()
    {
        SqlParameter[] parameters = new SqlParameter[1];
        parameters[0] = DataLayer.DataAccess.AddParameter("@Flag", Flag, System.Data.SqlDbType.Int, 20);
        DataTable dt = DataLayer.DataAccess.ExecuteDTByProcedure("SP_GetOrderList2", parameters);
        return dt;
    }

这里是存储过程:

ALTER procedure [dbo].[SP_GetOrderList2]
(
@Flag int
)
AS
BEGIN
    BEGIN TRY

    if(@Flag <>0)
    Begin
        Select *
        FROM CustomerDetails where Id=@Flag;

    End
    else
    begin
        select p.[name],cd.Id, cd.CustomerName, cd.CustomerEmailID,cd.CustomerPhoneNo,cd.CustomerAddress,cd.TotalPrice,cd.OrderDateTime, cd.PaymentMethod FROM CustomerDetails cd Inner Join CustomerProducts cp ON cp.CustomerID = cd.Id Inner Join Products p ON cp.ProductID = p.ProductID
    end


    END TRY

    BEGIN CATCH

        PRINT('Error Occured')
    END CATCH

    END

我在这里真正想要的是避免我的桌子上的冗余。我希望同一 ID 上的产品保持在一起,因为我的价格已经合并。任何帮助将不胜感激。

【问题讨论】:

  • 您应该规范化您的数据库以删除那些冗余。
  • 先生@WilliamXifaras 谢谢你

标签: asp.net sql-server stored-procedures


【解决方案1】:

您可以为此目的使用 group_concat() 函数。试试这个示例查询。

选择 id, name, phoneno, email, group_concat(product), price 来自&lt;TABLE_NAME&gt; 按电话号码分组

谢谢。

【讨论】:

【解决方案2】:

1

1>声明表变量

declare @Table Table(RecID int identity,Id int,Product nvarchar(50))

2>插入唯一值..

insert into @Table
select distinct id,Product from CustomerDetails 

3> 声明一些变量

declare @i int,@cnt int,@Id int
declare @Product VARCHAR(Max) 
select @cnt=count(*),@i=1 from @Table
while @i<=@cnt
begin
select @Id=id from @Table where RecID =@i
set @Product =''
SELECT  @Product = COALESCE(@Product + ',', '') + Product 
FROM CustomerDetails where id=@id
update @Table set Product =@Product where id=@id
set @i=@i+1
end

4>用表变量选择你的表..

select distinct d.id,d.name,d.phoneno,d.email,t.product
from CustomerDetails d
join @Table t on t.id=d.id
.

【讨论】:

  • 您好,先生,您能根据我的代码给出答案吗?我不明白。请,谢谢你,先生
【解决方案3】:

好的。

您可以在 If 部分中添加新代码..

----your code
if(@Flag <>0)
    Begin
       declare @Product VARCHAR(Max)
       SELECT  @Product = COALESCE(@Product + ', ', '') + Product 
       FROM CustomerDetails where Id=@Flag;
        ----select your entire field except product column.  In product columns you have only select `---@Product`  
     select distinct id, name, phoneno, email ,@Product  product
      from CustomerDetails where Id=@Flag;

    End
-----your code

@Product 变量中,您的产品列数据将用逗号连接。根据id.

如果你也想要它在其他部分,那么你必须按照我之前的回答..

【讨论】:

    猜你喜欢
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2013-06-07
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多