Postgres SQL UPDATE基于前一个SELECT的结果

时间:2018-01-24 22:31:09

标签: sql postgresql sql-update

在Postgres 10中,我想要两次执行UPDATE。无论如何都要运行第一个UPDATE,更新alwaysupdate列。并且仅当下面的SELECT语句返回行计数0时才应运行第二个UPDATE,这意味着sometimesupdate仅在mytable mykey = 100 sometimesupdate null中的所有行时才会更新1}}设置为-- Run no matter what, updating 'alwaysupdate' update mytable set alwaysupdate = now() where keyA = 100 and keyB = 200 -- Check the number of rows where 'sometimesupdate' has a value select count(*) from mytable where keyB = 200 and sometimesupdate is not null -- If 'sometimesupdate' is null for all rows above, give it a value in this row update mytable set sometimesupdate = now() where keyA = 100 and keyB = 200

<option>(.*?)</option>

最有效的方法是什么?是否可以将它组合成一个SQL语句?否则多个语句包含在一个事务中?否则就是必要的功能。

3 个答案:

答案 0 :(得分:1)

试试这个

如果count等于零,则更新到now()否则保留有时更新的旧值

update mytable as A
 set alwaysupdate = now(),
 sometimesupdate = (case when (
       select count(*) from mytable as B where B.keyB = A.keyB 
       and sometimesupdate is not null) = 0 
    then now() 
  else sometimesupdate end)
where keyA = 100 and keyB = 200

或者如果你想更新那个没有有时更新并且keyb = 200的特定行,那么在下面做

UPDATE mytable
SET alwaysupdate = now(),
    sometimesupdate = (CASE
      WHEN keyB = 200 THEN CASE
          WHEN sometimesupdate IS NULL THEN now()
          ELSE sometimesupdate
        END
      ELSE sometimesupdate
    END)
WHERE keyA = 100
AND keyB = 200

答案 1 :(得分:1)

一种方法将逻辑放在from子句中:

update mytable
    set alwaysupdate = now(),
        sometimesupdate = (case when b.cnt = 0 then now() else sometimesupdate end)
    from (select count(*) from mytable where keyB = 200 and sometimesupdate is not null
         ) b
    where keyA = 100 and keyB = 200;

但是,not exists通常会有更好的效果:

update mytable
    set alwaysupdate = now(),
        sometimesupdate = (case when not exists (select 1 from mytable where keyB = 200 and sometimesupdate is not null)
                                then now()
                           end)
         ) b
    where keyA = 100 and keyB = 200;

答案 2 :(得分:1)

您可以使用链式CTE,并使第二次更新以EXISTS(...)为条件[NOT EXISTS()与COUNT()== 0]相同

  -- Run no matter what, updating 'alwaysupdate'
WITH u1 AS ( 
        UPDATE mytable 
        SET alwaysupdate = now() 
        WHERE keyA = 100 AND keyB = 200;
        RETURNING *
        )
UPDATE mytable u2
SET sometimesupdate = now() 
FROM u1
WHERE u1.keyA = u2.keyA -- 100
  AND u1.keyB = u2.keyB -- 200
        -- If 'sometimesupdate' is null for all rows below, give it a value in this row
        -- Check if there are any rows where 'sometimesupdate' has a value
AND NOT EXISTS (SELECT * 
        FROM mytable nx
        WHERE nx.keyB = u2.keyB -- 200
        AND sometimesupdate IS NOT NULL
        );