如果满足条件,则插入mySQL记录

时间:2013-08-23 13:33:44

标签: php mysql sql insert

我正在使用PHP和MySQL数据库为游戏创建一个高分表

我已经得到它,以便它插入你的分数,然后告诉你你的位置,但是为了防止用同一个人向我发送垃圾邮件,我想要检查以下内容,

if score from your IP exists and score > your_current_highscore
UPDATE score

这很简单,令人难以理解的是,不是'得分'而是2个变量,'TIME'(完成所需的时间)和'PERCENT'(他们完成了多少游戏)。< / p>

我不确定如何构建SQL STATEMENT,我是否需要

if time OR percent > current

if time AND percent > current

TIME将是占主导地位的SORT,其次是百分比

有人可以为我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

尝试(对表名进行元语言):

UPDATE score_table
SET    score = new_score
WHERE  score > your_current_highscore AND
       score in (SELECT score
                 FROM table_with_ips
                 WHERE ip = your_ip) AND
       user_id = your_user_id

如果score > your_current_highscore为false,则不会更新任何条目。

由于您有两个参数(时间和百分比)且第一个参数占主导地位,因此您可以检查这样的分数:

--- score > your_current_hightscore
+++ time > your_current_time OR
+++ (time = your_current_time AND percentage > your_current_percentage)

答案 1 :(得分:0)

如果

,此查询将更新记录
  1. 新时间更好,百分比更好或相等,或
  2. 新时间相同,但完成的百分比更好:

    UPDATE leaderboards
    SET time=[player new time],
        percent=[player new percent]
    WHERE ip=[player IP]
        AND (
            /*new time is better than old time AND new percent is better than or equal to old percent*/
            time > [player new time]
                AND percent <=[player new percent] /*maybe you don't want this condition, see next proposal*/
    
            /*OR new time is equal to old time AND new percent is better than old percent */
            OR (
            time =[player new time] 
                AND percent < [player new percent])
        )
    
  3. 或者,如果

    ,此查询将更新记录
    1. 新时间更好(无论百分比如何变化)或
    2. 新时间相同,但完成的百分比更好:

      UPDATE leaderboards
      SET time=[player new time],
          percent=[player new percent]
      WHERE ip=[player IP]
          AND (
              /*new time is better than old time, regardless of change in percent*/
              time > [player new time]
      
              /*OR new time is equal to old time AND new percent is better than old percent */
              OR (
              time =[player new time] 
                  AND percent < [player new percent])
          )