PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 23, 2022

[FIXED] How to resolve this syntax error in a postgresql sql query to update multiple rows in a single column

 October 23, 2022     postgresql, sql, sql-update     No comments   

Issue

Given the tableA in a Postgres database, I need to update multiple rows in the field column_a:

UPDATE tableA
SET
  column_a = CASE WHEN column_a = 'conserve' THEN column_a = 'fixed',
  column_a = CASE WHEN column_a = 'balance' THEN column_a = 'moderate',
  column_a = CASE WHEN column_a = 'balance growth' THEN column_a = 'moderate growth',
  column_a = CASE WHEN column_a = 'aggressive' THEN column_a = 'moderate/agressive';

but I'm getting the following syntax error:

Error: syntax error at or near ","


Solution

You can do it with one single CASE statement:

UPDATE tableA
SET
  column_a = CASE WHEN column_a = 'conserve'       THEN 'fixed'
                  WHEN column_a = 'balance'        THEN 'moderate'
                  WHEN column_a = 'balance growth' THEN 'moderate growth'  
                  WHEN column_a = 'aggressive'     THEN 'moderate/agressive'
             END;

Try it here.



Answered By - lemon
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing