【问题标题】:updating a column with a condition from another table in postgresql使用 postgresql 中另一个表的条件更新列
【发布时间】:2018-07-05 15:14:51
【问题描述】:

所以基本上我需要更新一列以在附近有另一个表的功能时返回 true

到目前为止我的查询看起来像

update tablea a set is_nearby = 
case when b.condition1 = 1 and st_dwithin(a.geom, b.geom, 100) then true
else false end
from tableb b

但是当我知道情况并非如此时,这只会返回所有错误

【问题讨论】:

  • 请发布具有预期结果的示例数据。

标签: sql postgresql join sql-update


【解决方案1】:

我觉得你需要exists:

update tablea a
    set is_nearby = (case when exists (select 1
                                       from tableb b
                                       where b.condition1 = 1 and st_dwithin(a.geom, b.geom, 100)
                          then true
                          else false
                      end);

或者更简单地说,没有case

update tablea a
    set is_nearby = (exists (select 1
                             from tableb b
                             where b.condition1 = 1 and st_dwithin(a.geom, b.geom, 100)
                    );

您的查询的问题是from 正在生成叉积,因此b 中的所有行都与a 中的每一行进行比较。但是,只设置了一个值——该值来自b 中的任意匹配行。

【讨论】:

    猜你喜欢
    • 2021-01-08
    • 2012-11-08
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    相关资源
    最近更新 更多