小编Y.B*_*.B.的帖子

如何使用2个联接进行“ FOR JSON”

我无法使它正常工作。我有4个表格:产品,供应商,X_Product_Suppliers和注释。我想全部查询它们,并使用以下查询将它们放入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 …
Run Code Online (Sandbox Code Playgroud)

sql-server json

2
推荐指数
2
解决办法
700
查看次数

标签 统计

json ×1

sql-server ×1