Mysql:Trim all fields in database

前端 未结 7 1732
一生所求
一生所求 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 08:56

    Since the question asks for the whole database, here is the script that generates the required SQL. I skip the auto execute, execute it as you like.

    -- Set your database name here
    SET @my_database:='YOUR_DB_NAME';
    
    SET SESSION group_concat_max_len = 1000000;
    
    SELECT 
        CONCAT('UPDATE `', @my_database, '`.`', TABLE_NAME, 
                '` SET ', GROUP_CONCAT(
                    CONCAT('`', COLUMN_NAME, '` = TRIM(`', COLUMN_NAME, '`)')
                    ORDER BY ORDINAL_POSITION ASC),
                ';') AS `query`
    FROM
        INFORMATION_SCHEMA.COLUMNS
    WHERE
        TABLE_SCHEMA = @my_database
    GROUP BY TABLE_NAME
    ORDER BY TABLE_NAME ASC;
    

    @ZweiStein Thanks.

提交回复
热议问题