gpt4 book ai didi

mysql - 在 MariaDB 中是否有等效的方法来执行 MySQL JSON_OBJECT()?

转载 作者:搜寻专家 更新时间:2023-10-30 20:07:43 28 4
gpt4 key购买 nike

我正在为一个数据项目工作的服务器崩溃了,我现在正在重新创建数据库。我以前在 MySQL 数据库上工作,现在我在使用 MariaDB。我以前从未使用过 MariaDB。

以前,我使用以下命令将一些数据从另一个表插入到另一个表中:

CREATE TABLE collaborators_list 
SELECT awards.id, awards.researcher_name, awards.organization_id,
JSON_OBJECTAGG(awards.fiscal_year, coapplicants.coapplicant_name,
coapplicants.organization_number)
AS 'coapplicants_list' FROM awards
INNER JOIN coapplicants
ON awards.id=coapplicants.id
GROUP BY awards.researcher_name, awards.organization_id;

基本上,我想在 MariaDB 中做同样的事情。我试着看这里: https://mariadb.com/kb/en/library/json-functions/但除非我误读了什么,否则这些都不是我真正想要的……

帮助!

最佳答案

,MariaDB 仍然不支持JSON_ARRAYAGGJSON_OBJECTAGG 函数。已为请求此功能提出了 JIRA 票证:https://jira.mariadb.org/browse/MDEV-16620

现在,来自 JSON_OBJECTAGG() 的文档:

It takes only two column names or expressions as arguments, the first of these being used as a key and the second as a value.

An error occurs if any key name is NULL or the number of arguments is not equal to 2.

但是,您在 JSON_OBJECTAGG(awards.fiscal_year, coapplicants.coapplicant_name, coapplicants.organization_number) 中指定了三个参数; 因此您尝试的查询将无法正常工作

现在,在缺少所需功能的情况下,我们可以利用 Group_Concat()Concat() .我假设您只需要前两个参数(如前一段所述)。

GROUP_CONCAT( DISTINCT CONCAT('"', awards.fiscal_year, '": "',
coapplicants.coapplicant_name, '"')
SEPARATOR ', ')

请注意,如果字符串变得非常非常长,Group_Concat() 可能 会截断它。因此,您可以通过在上述查询之前执行以下查询来增加允许的长度:

SET SESSION group_concat_max_len = @@max_allowed_packet;

关于mysql - 在 MariaDB 中是否有等效的方法来执行 MySQL JSON_OBJECT()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53236872/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com