【问题标题】:Update set with columns depending on another更新集的列取决于另一个
【发布时间】:2020-11-21 13:20:41
【问题描述】:

我正在尝试设置列值,例如 area,然后在以下集合中使用此 area 列值,例如 area * somecolumn。当我这样做时,似乎area 尚未设置。我是否需要为 postgres 做一些事情来注册/理解/提交下一个 set 语句才能工作?还是应该这样做?

update table1 t1
set
    area = something;
    --update/commit/save edits/?

update table1 t1
set
    volume = area * col1 #I belive area hasnt been set yet so volume become NULL. But when I look at the table afterwards, area has a value

and many more...

【问题讨论】:

    标签: sql postgresql sql-update


    【解决方案1】:

    您的查询应该按照您的意图进行,前提是“某事”和col1 不是null

    但是,您可以通过一条语句轻松地做您想做的事情:

    update table1 t1
    set area = $1, volume = $1 * col1
    

    【讨论】:

    • 很好,那我一定是做错了什么,我会试着找出来。美元符号是什么?
    • @BERA:这是一个绑定参数,表示您要传递给查询的值(您在问题中称其为“某事”)。
    • 美元符号用于按各自的顺序引用输入参数。
    • @BERA:我明白了。这称为整数除法。注意:我建议使用精确的数据类型(numericdecimal)而不是 real,这是不精确的。
    • @BERA:是的。如果有一个数字操作数,则整个操作都在数字上下文中执行。
    猜你喜欢
    • 2022-08-20
    • 2015-05-02
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多