简体   繁体   English

Postgresql更新

[英]Postgresql update

I am new to postgreql and sql itself.So how do I set a json value of 'class'?我是 postgreql 和 sql 本身的新手。那么如何设置“类”的 json 值? Official documentation and other resources provided me with the function jsonb_set which i used to set a hard coded value of '"X"', however I am not able to set a dynamic value based off of the student table's column.官方文档和其他资源为我提供了 function jsonb_set,我用它来设置“X”的硬编码值,但是我无法根据学生表的列设置动态值。

update dp_user_custom_properties_v2 ducpv
    set json_value = jsonb_set(ducpv.json_value, '{class}',s.class::text)
    from students s
    where s.user_id = ducpv.user_id and s.status is null;

this doesn't work but这行不通,但是

set json_value = jsonb_set(ducpv.json_value, '{class}','"A"')

this does.这确实。

This is happening because 3rd argument of jsonb_set() should be a jsonb and you are passing text.发生这种情况是因为jsonb_set()的第三个参数应该是 jsonb 并且您正在传递文本。 You should convert it to jsonb first.您应该先将其转换为 jsonb。 Use to_jsonb() like below.像下面这样使用to_jsonb()

update dp_user_custom_properties_v2 ducpv
    set json_value = jsonb_set(ducpv.json_value, '{class}',to_jsonb(s.class::text))
    from students s
    where s.user_id = ducpv.user_id and s.status is null;

I found another way of doing it looking thorugh online resources -我找到了另一种方法来查看在线资源 -

update dp_user_custom_properties_v2 ducpv
                        set json_value = ducpv.json_value||jsonb_build_object('section', s.section,'class', s.class)
                        from students s
                        where s.user_id = ducpv.user_id and s.status is null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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