简体   繁体   English

mysql更新行(如果存在)

[英]mysql update row if exists

quick mysql php question. 快速的MySQL PHP的问题。

i have aa $row['sentence'] and another $row['rank'] 我有一个$row['sentence']和另一个$row['rank']

i'm tryin to write a thing..that says..if you find ...these certain words in the $row['sentence'] .. insert or update the $row['rank'] by anything i specify.. 我正在试着写点东西..说..如果您发现...这些$row['sentence'] entent $row['sentence']中的某些单词..用我指定的内容插入或更新$row['rank'] 。 。

ex. 恩。 this sentence has badwords.. so the rank row ..will become 1 . 这句话有badwords ..所以rank row ..将变成1

ex2. EX2。 this sentence is clean so the rank row will become 5 这句话很干净,所以rank row将变为5

thanks..if i confused u ..tell..me i will try another way.. 谢谢..如果我困惑你..tell..me,我会尝试另一种方式..

You can create a table with each bad word as a record and then parse through it to create a larger sql statement. 您可以创建一个表,将每个坏词作为记录,然后对其进行解析以创建更大的sql语句。 This way, you can add onto the list of bad words, whatever they might be from the database and not have to manually enter each one for each like clause. 这样,您可以将坏词列表添加到数据库中的任何坏词列表中,而不必手动为每个like子句输入每个单词。

So for example: 因此,例如:

$some_array = array(<list of bad words retrieved from db>);

$query = "update mytable set rank = 1 where ";

foreach ($some_array as $item) {
  $query .= "sentence like '%$item%'and ";
}

//remove last "and" from end of $query string
echo $query;

So it will generate a sql statement like what nickf provided but with a long list of all the bad words. 因此,它将生成nickf提供的sql语句,但列出所有不好的单词。

update mytable set rank = 1 where sentence like '%bad word%' and sentence like ...

Also not efficient but it should accomplish what you need to some degree. 同样效率不高,但是它应该在某种程度上完成您需要的工作。

Try this: 尝试这个:

UPDATE myTable SET rank = 1 WHERE sentence LIKE "%bad words%";
UPDATE myTable SET rank = 5 WHERE sentence NOT LIKE "%bad words%";

That's a little simplistic obviously, and not even very efficient - it'd be quicker to set everything to rank - 5 and then change back just the ones you need. 显然,这有点简单,甚至不是很有效-将所有内容设置为5级,然后只更改所需的内容,会更快。 Anyway, I hope that's what you were after. 无论如何,我希望那是你的追求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM