简体   繁体   English

用IF语句插入INSERT INTO

[英]postgresql INSERT INTO with IF statement

In postgresql , how to INSERT values in a column.b from column.a from same table Where postgresql ,如何INSERTcolumn.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 does not insert values into columns. INSERT不会在列中插入值。 It inserts new rows into your table. 它将新行插入到表中。 Instead, you need to use an UPDATE statement . 相反,您需要使用UPDATE语句 You will also need some ifs inside . 您还需要在其中添加一些ifs

If the row already exists, you do not need an INSERT . 如果该行已经存在,则不需要INSERT You need an UPDATE like this: 您需要这样的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM