SQL更新语句

标签 sql postgresql join sql-update

我有一个返回我 ID 的查询

select id, default_code from product_product ou
where (select count(*) from product_product inr
where inr.default_code = ou.default_code) > 1 and ou.active = false

但是我在这个语句中遇到语法错误

update product_product ou
where (select count(*) from product_product inr
where inr.default_code = ou.default_code) > 1 and ou.active = false set uo.default_code = uo.default_code || 'A';

ERROR:  syntax error at or near "where"
LINE 2:     where (select count(*) from product_product inr

如何正确更新从第一个语句中检索到的 id

最佳答案

正确:

update
    product_product ou
set
    default_code = ou.default_code || 'A'
from
    (
        select default_code
        from product_product
        group by default_code
        having count(*) > 1
    ) inr
where
    not ou.active
    and ou.default_code = inr.default_code

关于SQL更新语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55722522/

相关文章:

将 Inf、NAN 和 -Inf 插入 sqlite3 数据库的 SQL 命令

sql - 在 Postgresql 中从 SELECT 插入多行

ruby-on-rails - ActiveRecord 和 SQL 因未知 OID 被当成 String 而返回不相同结果的情况

在数据条目的 2 个不同位置使用的表中同一列的 SQL 语法

mysql - 三表连接的结果不正确

php - 加入一个 View 与另一个 View 性能

php - PHP 脚本/目录有哪些权限?

mysql - 在mysql中的字段名称中的空格之间添加破折号

SQL 创建 UDF 计算列

sql - Postgresql - 查询由另一列的代理索引的列