Changing all zeros (if any) across all columns (in a table) to… say 1

前端 未结 2 391
南旧
南旧 2021-01-19 23:58

I have a table with 18 columns (all Ints) and 1040 rows. If any value is zero I want to change it to 1. I am using Postgresql. What is the best way to do this. I cannot come

2条回答
  •  迷失自我
    2021-01-20 00:36

    How about this

    UPDATE table SET columnA = 1 WHERE columnA = 0
    

    But you will need a query for each column, or

    UPDATE table SET columnA = 
    CASE WHEN columnA = 0 THEN 1
    ELSE columnA
    END,
    
    columnB = 
    CASE WHEN columnB = 0 THEN 1
    ELSE columnB
    END, ...
    

提交回复
热议问题