我有个问题,我有一个varchar字段,它包含用逗号分隔的m个值:
garage 1,2
garage 1,3,5
我有以下疑问
select
json_object(
'data',
json_objectagg(
r.slug,
json_object(
'nome',r.name,
'localizacao',
json_object(
'endereco' , r.address,
'bairro' , r.neighborhood,
'cidade' , r.city,
'estado' , r.state,
'latitude' , r.lat,
'longitude' , r.lng
),
'tipo' , r.type,
'incorporadora' , d.name,
'dormitorios', json_object('legenda', r.rooms, 'quantidade', json_array(r.rooms_quantity)),
'garagem', json_object('legenda', r.garage, 'quantidade', json_array(r.garage_quantity)),
'metragem', json_object('legenda', r.footage, 'minimo', r.min_footage, 'maximo', r.max_footage),
'preco', json_object('minimo', IFNULL(r.min_value, 0), 'maximo' , IFNULL(r.max_value, 0)),
'capa' , ri.filename_default
)
)
) as jsonExport
from realties r
inner join developers d on r.developer_id = d.developer_id
inner join realties_images ri on r.realty_id = ri.realty_id and ri.type_image = 'cover'
where r.active = 'yes' and d.active = 'yes' and ri.active = 'yes';
但是当用整数返回
json_array
时,它与引号不匹配。就这样
["2,Studio"] ["1,2,3"]
但我需要它是一个数组,因为其他代码将获取信息,我需要它
["2","Studio"] ["1","2","3"]
我已经试过了,一切都很顺利,有人能帮我吗?
最佳答案
最好的解决方案是normalize数据库,避免在一个字段中存储多个值。
另一个解决方案是检索resultset并使用某种服务器脚本,对其进行处理,然后将其转换为JSON。
您可以编写自己的MySQL函数来分解逗号分隔的值,例如this。然而,我的建议是坚持1。从长远来看,这会节省你的时间。