1

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

我有它,它可以插入你的分数,然后告诉你你的位置,但为了防止与同一个人向董事会发送垃圾邮件,我希望它检查以下内容,

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

这很容易,但难点在于没有“分数”,而是有 2 个变量,“时间”(完成所需的时间)和“百分比”(他们完成了多少游戏)。

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

if time OR percent > current

或者;

if time AND percent > current

TIME 将是主要的 SORT,百分比为秒

任何人都可以为我简化这个吗?

4

2 回答 2

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)
于 2013-08-23T13:36:27.343 回答
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])
        )
    

或者,此查询将更新记录,如果

  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])
        )
    
于 2013-08-23T14:30:44.923 回答