为什么这个语句没有更新MySQL中的数据?

时间:2011-06-06 13:14:35

标签: php mysql

我是MySQL的新手。这段代码有什么问题?它不会更新数据。

"INSERT INTO highscores (name, score, maila, ip)" . "VALUES ('$name', '$score', '$maila', '$ip')" .
"ON DUPLICATE KEY UPDATE score;" . "UPDATE highscores SET (if score>'$score') {score=$score} WHERE name=$name"

这有效:

"INSERT INTO highscores (name, score, maila, ip) ".
     "VALUES ('$name', '$score', '$maila', '$ip') " . "
on duplicate key update score = greatest(score, $score)"

感谢 binaryLV MSQL: How to overwrite entry only if new one is higher? else create new entry

1 个答案:

答案 0 :(得分:1)

使用ON DUPLICATE KEY UPDATE时,还必须指定要更新的内容,即在PHP中,正确的查询将是:

$q = "INSERT INTO highscores (name, score, maila, ip) ".
     "VALUES ('$name', '$score', '$maila', '$ip') ".
     "ON DUPLICATE KEY UPDATE score='$score'";