我是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
答案 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'";