mysql 把多条符合条件的数组 json 合并成一条

select c.PROC_INST_ID_, sfj.file_json
from bpm_check_opinion c  
left join sys_file_json sfj on timestampdiff(minute,
        sfj.create_time,
        c.CREATE_TIME_) between 0 and 3 -- 三分钟内
where c.PROC_INST_ID_ = '10000005440002';
查询结果:
10000005440002	[{"id":"10000005440001","name":"2020-07-31_外卖.txt","size":"90"}]
10000005440002	[{"id":"10000005440001","name":"2020-07-31_外卖.txt","size":"90"}]
10000005440002	[{"id":"10000005440001","name":"2020-07-31_外卖.txt","size":"90"}]


合并SQLselect c.PROC_INST_ID_,
cast(concat('[', group_concat(concat('{"id":"', json_unquote(json_extract(sfj.file_json, '$[0].id')), '","name":"', 
    json_unquote(json_extract(sfj.file_json, '$[0].name')), '","size":"', 
    json_unquote(json_extract(sfj.file_json, '$[0].size')), '"}') 
    order by `sfj`.`create_time` asc separator ','), ']') as char(10000) charset utf8) as `files_json`
from bpm_check_opinion c  
left join sys_file_json sfj on timestampdiff(minute,
        sfj.create_time,
        c.CREATE_TIME_) between 0 and 3 -- 三分钟内
where c.PROC_INST_ID_ = '10000005440002'
group by c.PROC_INST_ID_;

查询结果:
10000005440002	[{"id":"10000005440001","name":"2020-07-31_外卖.txt","size":"90"},{"id":"10000005440001","name":"2020-07-31_外卖.txt","size":"90"},{"id":"10000005440001","name":"2020-07-31_外卖.txt","size":"90"}]

files_json 的 cast 一定要加,否则如果用来做更新,会报:
Data truncation: Data too long for column ‘files json’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值