select p.[name], 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
它会给我这个
现在我希望产品列的结果为
carbon dioxied,industrial oxygen
因为它们具有相同的ID。
请帮帮我。非常感谢你更新:当前声明,但仍然给我相同的结果。
use ShoppingCartDB
select
STUFF((SELECT ',' + p.[name]
FROM Products p
WHERE cp.ProductID = p.ProductID
FOR XML PATH(''),TYPE).value('.','nvarchar(max)'),1,1,'') AS Name
, 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
答案 0 :(得分:0)
SELECT DISTINCT
STUFF((SELECT ',' + p.[name]
FROM Products p
WHERE cp.ProductID = p.ProductID
FOR XML PATH(''),TYPE).value('.','nvarchar(max)'),1,1,'') AS Name
, 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