S
WITH
tmpProduct AS (
select p.product_id as id, p.image,
IF(count(t.image), JSON_ARRAYAGG(JSON_OBJECT('image', t.image)), JSON_ARRAY()) AS images
from oc_product p
LEFT JOIN LATERAL (
select image FROM oc_product_image pi
WHERE pi.product_id = p.product_id AND p.image != pi.image
ORDER BY pi.sort_order
) t ON true
where p.status = 1
group by p.product_id
)
select id, IF(image, JSON_ARRAY_APPEND(images, '$', image), images) as images
from tmpProduct
