PostgreSQL - 使用子查询更新多个列值
我需要能够使用子查询的结果更新表上的多个列。一个简单的示例如下所示 -
UPDATE table1
SET (col1, col2) =
((SELECT MIN (ship_charge), MAX (ship_charge) FROM orders))
WHERE col4 = 1001;
如何在 PostgreSQL 中执行此操作?
感谢您的任何提示!
更新:我很抱歉使示例对于我的实际用例来说过于简单。下面的查询更准确 -
UPDATE table1
SET (TOTAL_MIN_RATE, TOTAL_MAX_RATE) = (SELECT AVG(o.MIN_RATE), AVG(o.MAX_RATE)
FROM ORDR o INNER JOIN table2 ba ON (o.PAY_ACCT_ID = ba.ACCT_ID)
INNER JOIN table3 mb ON (ba.BANK_ID = mb.BANK_ID)
WHERE ba.CNTRY_ID = table1.CNTRY_ID AND
o.STUS_CD IN ('01','02','03','04','05','06') AND
((o.FRO_CRNCY_ID = table1.TO_CRNCY_ID AND o.TO_CRNCY_ID = table1.FRO_CRNCY_ID) OR
(o.TO_CRNCY_ID = table1.TO_CRNCY_ID AND o.FRO_CRNCY_ID = table1.FRO_CRNCY_ID))
GROUP BY ba.CNTRY_ID)
I need to be able to update multiple columns on a table using the result of a subquery. A simple example will look like below -
UPDATE table1
SET (col1, col2) =
((SELECT MIN (ship_charge), MAX (ship_charge) FROM orders))
WHERE col4 = 1001;
How can I do this in PostgreSQL ?
Thanks for any tips!
UPDATE: I apologize for making the sample too simple for my actual use-case. The query below is more accurate -
UPDATE table1
SET (TOTAL_MIN_RATE, TOTAL_MAX_RATE) = (SELECT AVG(o.MIN_RATE), AVG(o.MAX_RATE)
FROM ORDR o INNER JOIN table2 ba ON (o.PAY_ACCT_ID = ba.ACCT_ID)
INNER JOIN table3 mb ON (ba.BANK_ID = mb.BANK_ID)
WHERE ba.CNTRY_ID = table1.CNTRY_ID AND
o.STUS_CD IN ('01','02','03','04','05','06') AND
((o.FRO_CRNCY_ID = table1.TO_CRNCY_ID AND o.TO_CRNCY_ID = table1.FRO_CRNCY_ID) OR
(o.TO_CRNCY_ID = table1.TO_CRNCY_ID AND o.FRO_CRNCY_ID = table1.FRO_CRNCY_ID))
GROUP BY ba.CNTRY_ID)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这不是最有效的方法,但很简单:
This isn't the most efficient way to do this, but it's simple:
一种选择(但不是唯一的选择)是使用两个单独的子查询:
来自 PostgreSQL 9.0的更新精细手册:
One option (but not the only one) is to use two separate sub-queries:
From the fine manual for PostgreSQL 9.0's UPDATE:
当您没有简单的子选择时,使用 UPDATE FROM 是一个很好的解决方案。在此
UPDATE
中,我想将photos
表的event_profile_id
设置为照片集的所有者(事件配置文件也是所有者)照片属于.Using
UPDATE FROM
is a good solution when you don't have simple subselects. In thisUPDATE
I wanted to set theevent_profile_id
of thephotos
table to be the owner (event profiles are owners too) of the photo set the photo belongs to.正如官方文档所说:您可以使用标准更新PostgreSQL更新概要
所以你可以使用这样的写法:
或者更简单的方法:
As the official document says: you can use the standard update Synopsis of PostgreSQL update
So you can use write like this:
Or a more simple way:
我需要在一个表上进行多次插入,从两个表中获取数据,而它们之间没有公共列,并忽略已经存在的记录。
以下 sql 在 Postgresql 11 上进行了测试,尽管它在 v9+ 上应该可以正常工作:
I needed to do multiple inserts on a table taking the data from two tables without common columns between them and ignoring records already present.
The following sql was tested on Postgresql 11, althought it should work fine on v9+:
如果您想避免两个子选择,可以像这样重写查询:
如果ship_charge没有索引,这应该比两个子选择更快。如果ship_charge被索引,它可能不会产生很大的差异
编辑
从Postgres 9.5开始,这也可以写成:
If you want to avoid two subselects, the query can be rewritten like this:
If ship_charge is not indexed, this should be faster than two subselects. If ship_charge is indexed, it probably doesn't make a big difference
Edit
Starting with Postgres 9.5 this can also be written as:
如果您想一次更新 table1 中的多行,您还可以在子查询中返回多行。
You can also return multiple rows in the subquery if you want to update multiple rows at once in table1.