JSON_TABLE 的参数不正确

Incorrect arguments to JSON_TABLE

我的一个 table 中有一行 json 数据。现在我想以编程方式将数据 select 转换为 table。现在我只有在提供原始 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,"price_is_taxable":true},{"attribute_name":"size","attribute_value":"small","buy_price":300.0,"sell_price":500.0,"price_is_taxable":true}]

您需要 加入 使用 set-returning 函数选择 json 数组的查询 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'