问题描述
我的一张表中连续有json数据。现在,我想以编程方式将数据选择到表中。现在,只有提供原始的json数据,我才能实现它。
我现在不知道的是:
SELECT
attribute_name,attribute_value
FROM
JSON_TABLE ( '[json data should be fetched here from a table by using select]','$[*]' COLUMNS ( attribute_name VARCHAR ( 255 ) PATH '$.attribute_name',attribute_value VARCHAR ( 255 ) PATH '$.attribute_value' ) ) AS attributes_table
SELECT
attributes
FROM
tbl_items_pricing_attributes
WHERE
tbl_items_pricing_attributes.branch_id = '1001'
AND tbl_items_pricing_attributes.sku_code = '1000010003'
查询结果为:
[{"attribute_name":"size","attribute_value":"large","buy_price":500.0,"sell_price":700.0,"price_is_taxable":true},{"attribute_name":"size","attribute_value":"medium","buy_price":400.0,"sell_price":600.0,"attribute_value":"small","buy_price":300.0,"sell_price":500.0,"price_is_taxable":true}]
解决方法
您需要 join 使用设置返回功能json_table()
选择json数组的查询:
SELECT jt.attribute_name,jt.attribute_value
FROM tbl_items_pricing_attributes pa
CROSS JOIN JSON_TABLE (
pa.attributes,'$[*]' COLUMNS (attribute_name VARCHAR(255) PATH '$.attribute_name',attribute_value VARCHAR(255) PATH '$.attribute_value')
) jt
WHERE pa.branch_id = '1001' AND pa.sku_code = '1000010003'