How do I change all empty strings to NULL in a table?

前端 未结 7 1508
悲哀的现实
悲哀的现实 2020-12-08 20:25

I have a legacy table with about 100 columns (90% nullable). In those 90 columns I want to remove all empty strings and set them to null. I know I can:

updat         


        
7条回答
  •  一向
    一向 (楼主)
    2020-12-08 20:53

    One possible script:

    for col in $(echo "select column_name from information_schema.columns
    where table_name='$TABLE'"|mysql --skip-column-names $DB)
    do
    echo update $TABLE set $col = NULL where $col = \'\'\;
    done|mysql $DB
    

提交回复
热议问题