I have the following sql query that works fine using GROUP_CONCAT
:
SELECT orders.created_at,products.title, o_p.qty AS qty,
(SELECT GROUP_CONCAT(features.title,"\:", o_p_f.value, features.unit) FROM order_product_features AS o_p_f
LEFT JOIN product_features ON product_features.id = o_p_f.product_feature_id
LEFT JOIN features ON o_p_f.product_feature_id = product_features.id
AND features.id = product_features.feature_id
WHERE o_p_f.order_product_id = o_p.id
) AS prop
FROM orders
LEFT JOIN order_products AS o_p ON o_p.order_id = orders.id
LEFT JOIN products ON products.id = o_p.product_id
The above query returns result looks like the following screen shot:
Now, I want to replace GROUP_CONCAT
with JSON_OBJECT
or in other words, I want to have prop
field to be JSON object. I have tried the following:
SELECT orders.created_at,products.title, o_p.qty AS qty,
JSON_OBJECT((SELECT GROUP_CONCAT("title",features.title,"value", o_p_f.value, 'unit',features.unit) FROM order_product_features AS o_p_f
LEFT JOIN product_features ON product_features.id = o_p_f.product_feature_id
LEFT JOIN features ON o_p_f.product_feature_id = product_features.id
AND features.id = product_features.feature_id
WHERE o_p_f.order_product_id = o_p.id
)) AS prop
FROM orders
LEFT JOIN order_products AS o_p ON o_p.order_id = orders.id
LEFT JOIN products ON products.id = o_p.product_id
However, the above query returns error:
1582 - Incorrect parameter count in the call to native function 'JSON_OBJECT'
prop
field to be JSON object" i.e I need a way to transform the prop field into a JSON object. Any example you will supply that useGROUP_CONCAT
then using any way to transform that concatenated string into JSON will be very enough to solve my problem. – Timid