繁体   English   中英

使用 json_arrayagg 重复数据

[英]Data get repeated with json_arrayagg

从查询ID中得到的output重复了它里面的数据是多个

select json_arrayagg(json_object("network", basics_profiles.network, "username", basics_profiles.username, "url", basics_profiles.url)) as profiles from basics_profiles;

好的,当我在工作部分运行此代码时,即使它只有 2 个数据。 它加倍。 如果只有一个数据。 那么就没有问题了。

output 同时还有更多的 tan 1 数据:

[
    {
        "url": "twitter.com/naruto",
        "network": "Twitter",
        "username": "uzumakinaruto"
    },
    {
        "url": "twitter.com/naruto",
        "network": "Twitter",
        "username": "uzumakinaruto"
    },
    {
        "url": "instagram.com/naruto",
        "network": "Instagram",
        "username": "uzumakinaruto"
    },
    {
        "url": "instagram.com/naruto",
        "network": "Instagram",
        "username": "uzumakinaruto"
    }
]

我想要这样的东西

[
    {
        "url": "twitter.com/naruto",
        "network": "Twitter",
        "username": "uzumakinaruto"
    },
    {
        "url": "instagram.com/naruto",
        "network": "Instagram",
        "username": "uzumakinaruto"
    }
]

您可以尝试使用 thsi 表:

create table basics_profiles(network text, url text, username text);

insert into basics_profiles values("twitter","jhaajdka.com","naruto");

insert into basics_profiles values("instagram","jhasdasdasdsdd.com","sasuke"); 

使用给定的信息,您可以尝试(由于缺少输入数据而无法测试):

select 
   json_arrayagg(DISTINCT json_object("network", basics_profiles.network, 
                             "username", basics_profiles.username, 
                             "url", basics_profiles.url)
                ) as profiles 
from basics_profiles;

编辑:

  • 使用 MariaDB 10.6 甚至不需要 DISTINCT,请参阅: dbfiddle
  • 使用 MySQL8.0 甚至不需要 DISTINCT,请参阅: dbfiddle

这证明需要输入数据来重现问题

谢谢你的协助。 我有我的问题。 使用 about 代码,如果我有多行数据,它会在一个 dingle 行中显示所有数据,对我来说,问题是当加入 2 个表时,数据会再次重复。

select basics_information.*,(select json_arrayagg(json_object("name", interests.name, "keywords", interests.keywords)) from interests where basics_information.id = interests.resumeId) as interests from basics_information left join interests on basics_information.id = interests.resumeId group by basics_information.id;

暂无
暂无

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

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