简体   繁体   中英

Incorrect arguments to JSON_TABLE

I have json data in a row in one of my tables. Now I want to programmatically select the data into a table. Right now I can achieve it only when I supply the raw json data.

What I have don now is:

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

The query to get the data is:

SELECT
    attributes 
FROM
    tbl_items_pricing_attributes 
WHERE
    tbl_items_pricing_attributes.branch_id = '1001' 
    AND tbl_items_pricing_attributes.sku_code = '1000010003'

The result of the query is:

[{"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,"price_is_taxable":true},{"attribute_name":"size","attribute_value":"small","buy_price":300.0,"sell_price":500.0,"price_is_taxable":true}]

You need to join the query that selects the json array with set-returning function json_table() :

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM