Mysql:Trim all fields in database

十年热恋 提交于 2019-12-09 07:46:34

问题


UPDATE mytable SET mycolumn= LTRIM(RTRIM(mycolumn));

works fine on trimming columns removing trailer spaces, but how can i adjust it to trim all columns without having to write each column name in table ?? cause i kind have a huge database.


回答1:


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;



回答2:


you expand the query for each column:

UPDATE mytable
SET mycolumn = LTRIM(RTRIM(mycolumn)),
    mycolumn2 = LTRIM(RTRIM(mycolumn2)),
    ...;



回答3:


UPDATE mytable SET 
mycolumn = LTRIM(RTRIM(mycolumn)), 
mycolumn2 = LTRIM(RTRIM(mycolumn2)) 

and so on, and so forth.




回答4:


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.




回答5:


If there are not too many columns, you could just
directly UPDATE each by your_column_name, via the TRIM() function:

UPDATE mytable SET 
mycolumn1 = TRIM(mycolumn1), 
mycolumn2 = TRIM(mycolumn2),
mycolumn3 = TRIM(mycolumn3),
mycolumn4 = TRIM(mycolumn4)

Otherwise, ZweiStein's answer above for a single table,
or Izhar Aazmi's answer for an entire database seem the way to go.

Hiram's answer to another SO Post includes a check to only TRIM VARCHAR fields: excellent feature!

Or, if using T-SQL, or others which do not support TRIM, use the LTRIM(RTRIM(...)) trick,
suggested by Jim Rubenstein and Denis de Bernardy above.




回答6:


You can use PHP for it ( in order to avoid sql errors, better print queries then execute them later ) :

$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'database';
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
$db->set_charset("utf8");

$queries = '';
$query="SELECT * from table";
$result = $db->query($query);
$headers = $result->fetch_fields();
foreach($headers as $header) {
        $col = $header->name;

       $queries .= "UPDATE table SET `".$col."` = TRIM(`".$col."`) </br>";
}
echo $queries;

?>



来源:https://stackoverflow.com/questions/6072197/mysqltrim-all-fields-in-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!