Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将变量放入postgresql查询中的json_extract_path_文本中_Sql_Json_Postgresql_Postgresql 10 - Fatal编程技术网

将变量放入postgresql查询中的json_extract_path_文本中

将变量放入postgresql查询中的json_extract_path_文本中,sql,json,postgresql,postgresql-10,Sql,Json,Postgresql,Postgresql 10,我有以下选择: select json_extract_path_text(rules, 'amount', '5', 'percentage') from promotion_rules 来自JSON的示例如下所示: { "amount": { "1": { "percentage": 1 }, "2": { "percentage": 3 }, "3":

我有以下选择:

select json_extract_path_text(rules, 'amount', '5', 'percentage')
from promotion_rules
来自JSON的示例如下所示:

{
    "amount": {

        "1": {
            "percentage": 1
        },
        "2": {
            "percentage": 3
        },
        "3": {
            "percentage_below_eq": 5,
            "percentage_above": 10,
            "price": 20
        },
        "4": {
            "percentage_below_eq": 10,
            "percentage_above": 15,
            "price": 20
        }
    }
}
我想使用json_extract函数中的其他查询/表/cte中的值,而不是“5”(或实现精确效果),如何实现

这是代码的一部分,处理完整的数据,我不能把它们全部放在这里,因为stack告诉我我的文章主要是代码

with percentages as (select pr.*, json_object_keys(rules->'amount')::INT as amount
from
promotion_rules pr
where id = 1
)
select
o.id as order_id,
json_extract_path_text(rules, 'amount', o.products_no, 'percentage') as percentage --it doesn't work this way, either with brackets
from orders o
join percentages p on p.amount = o.products_no

json\u extract\u path
需要一个
文本
参数列表

如果要使用非文本的列,则需要强制转换该列:

json_extract_path_text(rules, 'amount', o.products_no::text, 'percentage')
json_extract_path_text(rules,'amount',o.products_no::text,'percentage')是的,我指的是来自某个表的值,或者cte-correct main postNo,它不是这样工作的,如果我把它放在括号中,它将被视为json中的潜在键(path),如果没有括号,它将生成一个错误:error:function json_extract_path_text(json,未知,整数,未知)不存在