2013-01-04 165 views
0

对不起 - 我是一个完整的新手,所以任何帮助非常感谢。删除MySQL数据库中的字符

我有一个柱(texty3)在MySQL数据库约30,000条目需要具有特定的字符/词语删除:

具体地说,

少于3个字符的任何单词。 任何非capilised单词。 任何非字母数字。

我在想知道是否有这样的mysql命令?

我有某人通过PHP,看起来像这样写日后上传脚本,

function getmtext($str) { 
    $text = ''; 
    $words = str_word_count($str, 1); 
    foreach ($words as $word) { 
     if ($word[0] >= 'A' && $word[0] <= 'Z') 
      if (strlen($word)>3) 
       $text .= $word.' '; 
    } 
    return $text; 
} 

但我不知道如何编辑已经存在的条目。

任何帮助表示赞赏!

+1

我认为你必须编写一个PHP脚本来批量处理数据库中的所有条目,MySQL没有内置的功能来做你正在寻找的东西。 – Stefan

回答

0

您可以使用相同的脚本来更新SQL中的条目,但我没有办法(我知道)按照您使用纯粹的SQL请求执行的操作。

即你可以从数据库中检索数据(使用mysql函数),修改信息并将其返回给数据库。

为了讨论如果您有本地数据库,名为test没有密码以root身份登录,你可以使用类似:

function getmtext($str) { 
    $text = ''; 
    $words = str_word_count($str, 1); 
    foreach ($words as $word) { 
     if ($word[0] >= 'A' && $word[0] <= 'Z') 
     if (strlen($word)>3) 
      $text .= $word.' '; 
    } 
    return $text; 
} 

//set the database parameters 
$dbHost = "localhost"; 
$dbUser = "root"; 
$dbPass = ""; 
$dbName = "test"; 

//create the database connection 
$dbConn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error()); 
mysql_select_db($dbName) or die('Cannot select database. ' . mysql_error()); 

//sql to get all texty3 rows from table 
$sql = "select id, texty3 from testtable"; 
$query = mysql_query($sql); //run the sql 
while($result = mysql_fetch_assoc($query)) { //loop through each row 
    $rowId = $result['id']; 
    $text = $result['texty3']; 
    $cleansedText = getmtext($text); 
    mysql_query("update testtable set texty3 = '$cleansedText' where id = '$rowId';"); 
} 

这将经历数据库,并使用该函数删除所有单词与函数中定义的参数不匹配。 (对不起,我无法在一分钟内检查此代码,但它应该是正确的或至少接近)

+0

令人惊叹!谢谢! –