i have an e commerce site and a single ID can have a multiple product orders. As of now i have this:
as you can see, it shows redudancy on my table. I want to have an output like:
Carbon Dixode, Industrial Oxygen
since they are on the same ID (10). same goes with
Compressed Air, Kerosene, Medical Oxygen
here is my code behind:
private void GetOrderList()
{
ShoppingCart k = new ShoppingCart()
{
Flag = 0
};
DataTable dt = k.GetOrderList();
gvCustomerOrders.DataSource = dt;
gvCustomerOrders.DataBind();
gvCustomerOrders.HeaderRow.TableSection = TableRowSection.TableHeader;
}
this is the 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;
}
and here is the stored procedure:
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
what i really want here is to avoid redudancy in my table. i want the products on the same ID stays together since my price are already combined. Any help will be greatly appreciated.
3条答案
按热度按时间mwkjh3gx1#
You can use group_concat() function for this purpose. Try this sample query.
SELECT id, name, phoneno, email, group_concat(product), price FROM
<TABLE_NAME>
GROUP BY phonenoThank You.
zsohkypk2#
Ok.
You can put new code in your If secttion..
In
@Product
variable your product column data will be concatenate by comma. according toid
.If You also want it in else section then you have to follow my previous answer..
zqry0prt3#
1>DECLARE Table Variable
2>Insert it unique value..
3> Declare some variable
4>Select ur table with table variable..