更新表mysql,如果值为10则更新另一个表

时间:2013-03-13 14:27:35

标签: mysql sql

我按照sql更新结果表:

$mysqli->query("UPDATE results
                SET result_value = IF('$logo_value' - result_tries < 0 OR '$logo_value' - result_tries = 0, 1, '$logo_value' - result_tries)
                WHERE logo_id = '$logo_id'
                AND user_id = '$user_id'
                AND result_value = 0");

在同一个sql命令中,是否可以根据result_value更新另一个表?

if result_value = 10
Update users SET user_hints = user_hints +1 WHERE user_id = '$user_id'

我如何将其合并到上面的sql语法中?

我能想到的很长的路要是选择这个值将它变成php变量。并且比基于php变量值的另一个更新...但这看起来漫长而乏味

1 个答案:

答案 0 :(得分:0)

这是一个很长的镜头(未经测试),但如何:

$mysqli->query("UPDATE results, users
                SET result_value = 
                        IF('$logo_value' - results.result_tries < 0 OR 
                           '$logo_value' - results.result_tries = 0, 
                            1, '$logo_value' - result_tries),
                    users.user_hints = 
                        IF(results.result_value >= 10, 
                           users.user_hints + 1, users.user_hints)
                WHERE results.logo_id = '$logo_id'
                AND results.user_id = '$user_id'
                AND results.user_id = users.user_id
                AND results.result_value = 0");

如果两个表都有一些相同的列名,当然,您必须指定哪个表(如results.user_id - 或 - users.user_id