一尘不染

SQL UPDATE语句

sql

我有此查询返回我的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


阅读 609

收藏
2021-03-08

共1个答案

一尘不染

正确的:

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
2021-03-08