0

申し訳ありませんが、私は完全な初心者なので、どんな助けでも大歓迎です。

mysqlデータベースの列(texty3)に約30,000のエントリがあり、特定の文字/単語を削除する必要があります。

具体的には、

3文字より短い単語。大文字でない単語。英数字以外のもの。

このための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;
}

しかし、すでに存在するエントリを編集する方法がわかりません。

助けていただければ幸いです。

4

1 に答える 1

0

同じスクリプトを使用してSQLのエントリを更新できますが、純粋なSQLを使用して要求したように(私が知っている)実行する方法はありません。

つまり、(mysql関数を使用して)データベースからデータを取得し、情報を変更してデータベースに返すことができます。

引数として、rootとしてログインするパスワードなしでtestという名前のデータベースがローカルにある場合は、次のように使用できます。

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';");
}

これはデータベースを調べ、その関数を使用して、関数で定義されたパラメーターと一致しないすべての単語を削除します。(申し訳ありませんが、このコードをすぐに確認することはできませんが、正しいか、少なくとも近いはずです)

于 2013-01-04T17:52:25.350 に答える