GROUP BY在UPDATE FROM子句中

编程入门 行业动态 更新时间:2024-10-28 12:30:31
本文介绍了GROUP BY在UPDATE FROM子句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我真的需要做这样的事情:

I really need do something like that:

UPDATE table t1 SET column1=t2.column1 FROM table t2 INNER JOIN table t3 USING (column2) GROUP BY t1.column2;

但是postgres说我有关于GROUP BY子句的语法错误。

But postgres is saying that I have syntax error about GROUP BY clause. What is a different way to do this?

推荐答案

UPDATE语句不支持GROUP BY,请参阅文档。如果你想用t2中相应的行更新t1,你可能想使用如下的WHERE子句:

The UPDATE statement does not support GROUP BY, see the documentation. If you're trying to update t1 with the corresponding row from t2, you'd want to use the WHERE clause something like this:

UPDATE table t1 SET column1=t2.column1 FROM table t2 JOIN table t3 USING (column2) WHERE t1.column2=t2.column2;

如果您需要在分配给t1之前将t2 / t3中的行分组,使用子查询,如下所示:

If you need to group the rows from t2/t3 before assigning to t1, you'd need to use a subquery something like this:

UPDATE table t1 SET column1=sq.column1 FROM ( SELECT t2.column1, column2 FROM table t2 JOIN table t3 USING (column2) GROUP BY column2 ) AS sq WHERE t1.column2=sq.column2;

尽管按照公式制定,但由于t2.column1不包含在GROUP BY语句中(它必须是一个集合函数,而不是一个简单的列引用)。

Although as formulated that won't work because t2.column1 isn't included in the GROUP BY statement (it would have to be an aggregate function rather than a simple column reference).

否则,你到底在做什么?

Otherwise, what exactly are you trying to do here?

更多推荐

GROUP BY在UPDATE FROM子句中

本文发布于:2023-10-14 21:30:36,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:句中   GROUP   UPDATE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!