PostgreSQL中更新语句中的内部联接

PostgreSQL中更新语句中的内部联接,第1张

概述我有一个名为temp_table的表,它由以下行组成: cola colb result ---------------- p4 s1 0 p8 s1 0 p9 s1 0 p5 f1 0 p8 f1 0 现在我需要使用colb的count(*)更新结果列.我正在尝试以下查询: update tem_tableset resu 我有一个名为temp_table的表,它由以下行组成:
cola colb result ----------------  p4   s1    0  p8   s1    0  p9   s1    0  p5   f1    0  p8   f1    0

现在我需要使用colb的count(*)更新结果列.我正在尝试以下查询:

update tem_tableset result = x.resultfrom tem_table ttinner join(select colb,count(*) as result from tem_table group by colb) xon x.colb = tt.colb;

并从temp_table中选择不同的colb和结果:

select distinct colb,result from tem_table;

获得输出:

colb result----------- s1    3 f1    3

但预期的产出是:

colb result----------- s1    3 f1    2

我没有得到我在查询中出错的地方?请帮帮我.谢谢

您不应该重复要在from子句中更新的表.这将创建一个笛卡尔自连接.

从手册中引用:

Note that the target table must not appear in the from_List,unless you intend a self-join (in which case it must appear with an alias in the from_List)

(强调我的)

不幸的是,UPDATE不支持使用JOIN关键字进行显式连接.像这样的东西应该工作:

update tem_table  set result = x.resultfrom (    select colb,count(*) as result     from tem_table     group by colb) xwhere x.colb = tem_table.colb;
总结

以上是内存溢出为你收集整理的PostgreSQL中更新语句中的内部联接全部内容,希望文章能够帮你解决PostgreSQL中更新语句中的内部联接所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1165907.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存