[英]postgresql INSERT INTO with IF statement
在postgresql
,如何INSERT
從column.a來自同一個表凡在column.b值
IF column.a = 1 then column.b = foo,
IF column.a = 2 then column.b = bar,
IF column.a = 3 then column.b = good,
IF column.a = 4 then column.b = bad
如果該行已經存在,則不需要INSERT
。 您需要這樣的UPDATE
:
UPDATE your_table
SET b = CASE
WHEN a = 1 then 'foo'
WHEN a = 2 then 'bar'
WHEN a = 3 then 'good'
ELSE 'bad'
END
WHERE some_condition = 'true';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.