1

In postgresql, how to INSERT values in a column.b from column.a from same table Where

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

2 Answers 2

5

INSERT does not insert values into columns. It inserts new rows into your table. Instead, you need to use an UPDATE statement. You will also need some ifs inside.

Sign up to request clarification or add additional context in comments.

Comments

3

If the row already exists, you do not need an INSERT. You need an UPDATE like this:

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';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.