如何在mysql中获得格式化的JSON,就像在sql中一样,我们使用FOR JSON子句



如何创建类似的查询

https://learn.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server?view=sql-服务器-2017

但在MySQL中,它在方括号上显示错误,如果我使用",它会给出输出

查询:

SELECT
max(cpm) as [cpm.1],
min(cpm) as [cpm.2]
FROM
b.s;

SELECT
max(cpm) as 'cpm.1',
min(cpm) as 'cpm.2'
FROM
b.s;

第一个错误第二个结果是

[
{
"cpm.1" : 10.91,
"cpm.2" : 10.91
}
]

结果应该是

[
{
"cpm" :
{
"1" : 10.91,
"2" : 10.91
}
}
]

您要查找的是JSON_OBJECT函数,你的代码应该像

SELECT
JSON_OBJECT("cpm", JSON_OBJECT("1",max(cpm),"2",
min(cpm)))
FROM
b.s;

如果您需要一些文档,请查看此链接。

最新更新