【问题标题】:Incorrect arguments to JSON_TABLEJSON_TABLE 的参数不正确
【发布时间】:2021-01-11 15:28:09
【问题描述】:

我的一张表中连续有 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,"price_is_taxable":true},{"attribute_name":"size","attribute_value":"small","buy_price":300.0,"sell_price":500.0,"price_is_taxable":true}]

【问题讨论】:

    标签: mysql sql arrays json unnest


    【解决方案1】:

    您需要加入带有集合返回函数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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-13
      • 2023-04-11
      • 2011-06-23
      • 2019-05-23
      • 2015-05-27
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多