Mysql:Trim all fields in database

前端 未结 7 1736
一生所求
一生所求 2021-02-12 08:20
UPDATE mytable SET mycolumn= LTRIM(RTRIM(mycolumn));

works fine on trimming columns removing trailer spaces, but how can i adjust it to trim all column

7条回答
  •  情深已故
    2021-02-12 09:01

    Some years late, but might help others: This code trims all fields of a the table "your_table". Could be expanded to work on the whole database in the same way....

    SET SESSION group_concat_max_len = 1000000;
    select concat('update your_table set ',group_concat(concat('`',COLUMN_NAME, '` = trim(`',COLUMN_NAME,'`)')),';')
       FROM INFORMATION_SCHEMA.COLUMNS
        WHERE table_name = 'your_table'
        into @trimcmd;
    
    prepare s1 from @trimcmd;
    execute s1;
    DEALLOCATE PREPARE s1;
    

提交回复
热议问题