Mysql:Trim all fields in database

前端 未结 7 1749
一生所求
一生所求 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:04

    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."`) 
    "; } echo $queries;

    ?>

提交回复
热议问题