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’

你可能感兴趣的:(mysql,json)