我想比较mysql上2个相似表的实际结果和预测。

真实

 id | data1| data2 |


用户

 id | data1| data2 | points


排行

 id | user| total points


我要执行以下操作:

 if (real.data1 = user.data1) AND (real.data2 = user.data2)
 update user set points=8 where id=1
 else if(real.data1 > user.data1) AND (real.data2 > user.data2)
 update user set points=4 where id=1
 else if (real.data1 = real.data2) AND (user.data1 = user.data2)
 update user set points=4 where id=1
  else if (real.data1 < user.data1) AND (real.data2 < user.data2)
  update user set points=4 where id=1
        else
            update user set points=0 where id=1
   sum all values from points and update ranking table


可能吗?

最佳答案

我相信以下内容可以解决您的问题的上半部分,但我尚未对其进行测试:

UPDATE `user` u
 INNER JOIN `real` r ON (u.id = r.id)
SET    u.points = IF(r.data1 = u.data1 and r.data2 = u.data2,
                     8,
                     IF(r.data1 > u.data1 and r.data2 > u.data2,
                        4,
                        IF(r.data1 = r.data2 and u.data1 = u.data2,
                           4,
                           IF(r.data1 < u.data1 and r.data2 < u.data2,
                              4,
                              0)
                          )
                       )
                    )


如果这没有意义,请参见有关IF语句的MySQL docs

09-05 12:23