MySQL : how to remove double or more spaces from a string?

前端 未结 11 1322
南方客
南方客 2020-12-03 08:12

I couldn\'t find this question for MySQL so here it is:

I need to trim all double or more spaces in a string to 1 single space.

For example: \"The  

11条回答
  •  囚心锁ツ
    2020-12-03 08:31

    If you are using php....

    try{
    $con = new PDO ("mysql:host=localhost;dbname=dbasename","root","");
    }
    catch(PDOException $e){
    echo "error".$e-getMessage();   
    }
    
    $select = $con->prepare("SELECT * FROM table");
    $select->setFetchMode(PDO::FETCH_ASSOC);
    $select->execute();
    
    while($data=$select->fetch()){ 
    
    $id = $data['id'];
    $column = $data['column'];
    
    $column = trim(preg_replace('/\s+/',' ', $column)); // remove all extra space
    
    
    $update = $con->prepare("UPDATE table SET column=:column WHERE id='$id'");
    $update->bindParam(':column', $column );
    $update->execute();
    
    // echo $column."
    "; }

提交回复
热议问题