Microsoft SQL server 中 joined table 的列去掉方括号和大括号
Remove the square and curly brackets from the column of joined table in Microsoft SQL server
我正在使用 Microsoft SQL 服务器 v14.0.1000
我正在使用以下查询将我的查询结果转换为 JSON :
SELECT TOP(5) dn.id AS ID,
dn.EventTimeStamp AS EventTimeStamp,
dn.ControllerId AS ControllerID,
JSON_QUERY((SELECT ControllerName
FROM Controllers
WHERE dn.ControllerId = Controllers.ControllerID
FOR JSON PATH, without_array_wrapper)) AS ControllerName
FROM DashboardNotifications dn
ORDER BY dn.ID DESC
FOR JSON PATH
结果如下 JSON :
[
{
"ID": 354,
"EventTimeStamp": "2022-05-17T05:35:25",
"ControllerId": 24,
"ControllerName": {
"ControllerName": "P25-SC-0233"
}
},
{
"ID": 353,
"EventTimeStamp": "2022-05-17T05:34:20",
"ControllerId": 17,
"ControllerName": {
"ControllerName": "P25-SC-0226"
}
},
{
"ID": 352,
"EventTimeStamp": "2022-05-17T05:33:50",
"ControllerId": 16,
"ControllerName": {
"ControllerName": "P25-SC-0225"
}
}
]
我已经能够从
中删除方括号
"ControllerName":[{"ControllerName":"P25-SC-0233"}],
虽然我想要的输出是:
[
{
"ID": 354,
"EventTimeStamp": "2022-05-17T05:35:25",
"ControllerId": 24,
"ControllerName": "P25-SC-0233"
},
{
"ID": 353,
"EventTimeStamp": "2022-05-17T05:34:20",
"ControllerId": 17,
"ControllerName": "P25-SC-0226"
},
{
"ID": 352,
"EventTimeStamp": "2022-05-17T05:33:50",
"ControllerId": 16,
"ControllerName": "P25-SC-0225"
}
]
我需要做哪些更改才能删除嵌套的 ControllerName 键?
我想你只是想要没有 FOR JSON
的子查询(然后你不需要 JSON_QUERY
来转义它)
SELECT TOP(5) dn.id AS ID,
dn.EventTimeStamp AS EventTimeStamp,
dn.ControllerId AS ControllerID,
(
SELECT c.ControllerName
FROM Controllers c
WHERE dn.ControllerId = c.ControllerID
) AS ControllerName
FROM DashboardNotifications dn
ORDER BY dn.ID DESC
FOR JSON PATH;
Ensure your subquery is guaranteed to return a maximum of one row
您也可以只使用连接
SELECT TOP(5) dn.id AS ID,
dn.EventTimeStamp,
dn.ControllerId,
c.ControllerName
FROM DashboardNotifications dn
LEFT JOIN
Controllers c ON dn.ControllerId = c.ControllerID
ORDER BY dn.ID DESC
FOR JSON PATH;
完全没有必要json_query
。将查询更改为:
SELECT TOP(5) dn.id AS ID, dn.EventTimeStamp AS EventTimeStamp, dn.ControllerId AS ControllerID, (
SELECT ControllerName FROM Controllers WHERE dn.ControllerId = Controllers.ControllerID
) AS ControllerName
FROM DashboardNotifications dn
ORDER BY dn.ID DESC
FOR JSON PATH
我正在使用 Microsoft SQL 服务器 v14.0.1000 我正在使用以下查询将我的查询结果转换为 JSON :
SELECT TOP(5) dn.id AS ID,
dn.EventTimeStamp AS EventTimeStamp,
dn.ControllerId AS ControllerID,
JSON_QUERY((SELECT ControllerName
FROM Controllers
WHERE dn.ControllerId = Controllers.ControllerID
FOR JSON PATH, without_array_wrapper)) AS ControllerName
FROM DashboardNotifications dn
ORDER BY dn.ID DESC
FOR JSON PATH
结果如下 JSON :
[
{
"ID": 354,
"EventTimeStamp": "2022-05-17T05:35:25",
"ControllerId": 24,
"ControllerName": {
"ControllerName": "P25-SC-0233"
}
},
{
"ID": 353,
"EventTimeStamp": "2022-05-17T05:34:20",
"ControllerId": 17,
"ControllerName": {
"ControllerName": "P25-SC-0226"
}
},
{
"ID": 352,
"EventTimeStamp": "2022-05-17T05:33:50",
"ControllerId": 16,
"ControllerName": {
"ControllerName": "P25-SC-0225"
}
}
]
我已经能够从
中删除方括号"ControllerName":[{"ControllerName":"P25-SC-0233"}],
虽然我想要的输出是:
[
{
"ID": 354,
"EventTimeStamp": "2022-05-17T05:35:25",
"ControllerId": 24,
"ControllerName": "P25-SC-0233"
},
{
"ID": 353,
"EventTimeStamp": "2022-05-17T05:34:20",
"ControllerId": 17,
"ControllerName": "P25-SC-0226"
},
{
"ID": 352,
"EventTimeStamp": "2022-05-17T05:33:50",
"ControllerId": 16,
"ControllerName": "P25-SC-0225"
}
]
我需要做哪些更改才能删除嵌套的 ControllerName 键?
我想你只是想要没有 FOR JSON
的子查询(然后你不需要 JSON_QUERY
来转义它)
SELECT TOP(5) dn.id AS ID,
dn.EventTimeStamp AS EventTimeStamp,
dn.ControllerId AS ControllerID,
(
SELECT c.ControllerName
FROM Controllers c
WHERE dn.ControllerId = c.ControllerID
) AS ControllerName
FROM DashboardNotifications dn
ORDER BY dn.ID DESC
FOR JSON PATH;
Ensure your subquery is guaranteed to return a maximum of one row
您也可以只使用连接
SELECT TOP(5) dn.id AS ID,
dn.EventTimeStamp,
dn.ControllerId,
c.ControllerName
FROM DashboardNotifications dn
LEFT JOIN
Controllers c ON dn.ControllerId = c.ControllerID
ORDER BY dn.ID DESC
FOR JSON PATH;
完全没有必要json_query
。将查询更改为:
SELECT TOP(5) dn.id AS ID, dn.EventTimeStamp AS EventTimeStamp, dn.ControllerId AS ControllerID, (
SELECT ControllerName FROM Controllers WHERE dn.ControllerId = Controllers.ControllerID
) AS ControllerName
FROM DashboardNotifications dn
ORDER BY dn.ID DESC
FOR JSON PATH