update all NULL fields MySQL

2020-08-09 08:19发布

I'd like to update all NULL fields in one table to 0. Of course

UPDATE mytable SET firstcol=0 WHERE firstcol IS NULL 

would do the job. But I wonder if there´s a smarter solution than just c&p this line for every column.

7条回答
闹够了就滚
2楼-- · 2020-08-09 09:07

ALTER TABLE dataBaseName.tableName ADD COLUMN columnX INT(20) NULL DEFAULT 1 AFTER columnY;

It does the following

  1. adds a new column columnX after columnY.
  2. sets its value to default 1 throughout the column columnX

           columnY             columnX 

         | cellValueA     |     1       |        
         | cellValueB     |     1       |
         | cellValueC     |     1       |
         | cellValueD     |     1       |
查看更多
登录 后发表回答