我无法让它正常工作。我有4个表:产品,供应商,X_Product_供应商和评论。我想查询它们并使用以下查询将它们放入JSON:
WITH Products (Id, Name, Price) As (
SELECT 1, 'First Product', 10
), Suppliers (Id, Name) As (
SELECT 1, 'Factory1' UNION ALL
SELECT 2, 'Factory2'
), Comments (Id, [Text], ProductId) As (
SELECT 1, 'Good Product', 1 UNION ALL
SELECT 2, 'Fantastic!' , 1
), X_Product_Supplier (ProductId, SupplierId) As (
SELECT 1, 1 UNION ALL
SELECT 1, 2
)
SELECT Products.*, Suppliers.*, Comments.* FROM Products
LEFT OUTER JOIN X_Product_Supplier ON X_Product_Supplier.ProductId = Products.Id
LEFT OUTER JOIN Suppliers ON X_Product_Supplier.SupplierId = Suppliers.Id
LEFT OUTER JOIN Comments ON Comments.ProductId = Products.Id
FOR JSON AUTO
出于某种原因,sql-server会将注释嵌套在供应商下而不是产品下:
{
"Id":1,
"Name":"First Product",
"Price":"10",
"Suppliers":[
{
"Id":1,
"Name":"Factory1",
"Comments":[ //THIS SHOULD BE UNDER PRODUCT, NOT SUPPLIER
{
"Id":1,
"Text":"Good Product",
"ProductId":1
},
{
"Id":2,
"Text":"Fantastic!",
"ProductId":1
}
]
},
{
"Id":2,
"Name":"Factory2",
"Comments":[ //THIS IS NOW DUPLICATE
{
"Id":1,
"Text":"Good Product",
"ProductId":1
},
{
"Id":2,
"Text":"Fantastic!",
"ProductId":1
}
]
}
]
}
我真正想要的是:
{
"Id":1,
"Name":"First Product",
"Price":"10",
"Suppliers":[
{
"Id":1,
"Name":"Factory1"
},
{
"Id":2,
"Name":"Factory2"
}
],
"Comments":[
{
"Id":1,
"Text":"Good Product",
"ProductId":1
},
{
"Id":2,
"Text":"Fantastic!",
"ProductId":1
}
]
}
我该怎么做?
答案 0 :(得分:4)
如果有人在寻找答案,这是我写的简单查询。根据您的架构更改查询,它应该提供适当的结构化结果。
SELECT Products.*,
(SELECT Suppliers.*
FROM Suppliers
WHERE Suppliers.Id = Products.SuppliersId
FOR JSON AUTO) As Suppliers,
(SELECT Comments.*
FROM Comments
WHERE Comments.ProductId = Products.Id
FOR JSON AUTO) As Comments
FROM Products
FOR JSON AUTO
答案 1 :(得分:0)
SQL Server会将结果表转换为JSON,其列的顺序与它们在SELECT
语句中的顺序相同。它不会对列分组做出任何假设。为了帮助它,可以为每个产品的JSON对象预先安排供应商和评论:
WITH Products (Id, Name, Price) As (
SELECT 1, 'First Product', 10
), Suppliers (Id, Name) As (
SELECT 1, 'Factory1' UNION ALL
SELECT 2, 'Factory2'
), Comments (Id, [Text], ProductId) As (
SELECT 1, 'Good Product', 1 UNION ALL
SELECT 2, 'Fantastic!' , 1
), X_Product_Supplier (ProductId, SupplierId) As (
SELECT 1, 1 UNION ALL
SELECT 1, 2
)
SELECT Products.*,
(SELECT Suppliers.*
FROM Suppliers
INNER JOIN X_Product_Supplier ON X_Product_Supplier.SupplierId = Suppliers.Id
WHERE X_Product_Supplier.ProductId = Products.Id
FOR JSON AUTO, TYPE) As Suppliers,
(SELECT Comments.*
FROM Comments
WHERE Comments.ProductId = Products.Id
FOR JSON AUTO, TYPE) As Comments
FROM Products
FOR JSON AUTO
答案 2 :(得分:0)
我知道它来晚了,但是我遇到了同样的问题,但是答案并不能帮助我过滤下面的解决方案where子句中的空值
SELECT Products.*,
Suppliers.Id as [Suppliers.Id],
Suppliers.Name as [Suppliers.Name],
Comments.Id as [Comments.Id],
Comments.Text as [Comments.Text],
Comments.ProductId as [Comments.ProductId]
FROM Products
LEFT OUTER JOIN X_Product_Supplier ON X_Product_Supplier.ProductId = Products.Id
LEFT OUTER JOIN Suppliers ON X_Product_Supplier.SupplierId = Suppliers.Id
LEFT OUTER JOIN Comments ON Comments.ProductId = Products.Id
FOR JSON PATH
经过测试并在SQL SERVER 2017上工作