简体   繁体   English

对 JSON_OBJECT 和 DISTINCT 的 SQL 查询

[英]SQL query to JSON_OBJECT and DISTINCT

I'm trying to output a query result as a JSON object using JSON_ARRAYAGG(JSON_OBJECT()) (that works just fine).我正在尝试使用JSON_ARRAYAGG(JSON_OBJECT())将查询结果输出为 JSON 对象(效果很好)。 I can't use DISTINCT within the JSON_OBJECT command and using GROUP BY will not output the whole result as one JSON but as many rows (each then a JSON).我不能在 JSON_OBJECT 命令中使用DISTINCT并且使用GROUP BY不会将整个结果作为一个 JSON 输出,而是作为多行(然后是 JSON)输出。 Example:例子:

SELECT JSON_ARRAYAGG(JSON_OBJECT(\'id\', `id`, ...)) FROM table

That works fine but doesn't filter out the duplicates.这工作正常,但不会过滤掉重复项。 I tried :我试过 :

SELECT JSON_ARRAYAGG(JSON_OBJECT(\'id\', `id`, \'title\', DISTINCT(`title`), ...)) FROM table

And :和 :

SELECT JSON_ARRAYAGG(JSON_OBJECT(\'id\', `id`, \'title\', `title`, ...)) FROM table GROUP BY `title`

But the former is wrong (syntax wise) and the latter splits the result in multiple rows.但前者是错误的(语法明智),后者将结果拆分为多行。 Can someone explain me how to achieve this?有人可以解释我如何实现这一目标吗? Like :喜欢 :

SELECT JSON_ARRAYAGG(JSON_OBJECT(\'id\', `id`, ...)) FROM table

But with unique results only.但只有独特的结果。

Can you wrap the duplicated query as a subquery and distinct from it?您可以将重复的查询包装为子查询并与之不同吗?

select distinct xyz from ( SELECT JSON_ARRAYAGG(JSON_OBJECT(\\'id\\', id , ...)) as xyz FROM table) table1 ); select distinct xyz from ( SELECT JSON_ARRAYAGG(JSON_OBJECT(\\'id\\', id , ...)) as xyz FROM table) table1 ); select distinct xyz from ( SELECT JSON_ARRAYAGG(JSON_OBJECT(\\'id\\', , ...)) as xyz FROM table) table1 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM