0

排名表

  • ID,主键
  • RANK,等级或级别,1 为最高,3 为最低
  • MIN_SCORE,达到排名所需的最低点数或 XP
  • NAME,等级的关联名称

    Rank Table
    +----+------+-----------+-------------------------+
    | ID | RANK | MIN_SCORE |          NAME           |
    +----+------+-----------+-------------------------+
    |  1 |    1 |     18932 | Editor-in-Chief         |
    |  2 |    2 |     15146 | Senior Technical Writer |
    |  3 |    3 |     12116 | Senior Copywriter       |
    +----+------+-----------+-------------------------+
    

排名表

  • ID,主键
  • FK_MEMEBER_ID,成员主键的外键
  • FK_RANK,作者排名表排名列的外键(顶部)
  • SCORE,会员当前获得的分数或 XP

    Ranking Table
    +-----+--------------+---------+-------+
    | ID  | FK_MEMBER_ID | FK_RANK | SCORE |
    +-----+--------------+---------+-------+
    |   1 |            1 |       1 | 17722 |
    |   2 |            2 |       2 | 16257 |
    |   3 |            3 |       3 | 12234 |
    +-----+--------------+---------+-------+
    

在我的课堂上,我存储了排名——与排名表中的排名相匹配——并关联了最低分数;RANK 作为键,MINIMUM_SCORE 作为值。

当成员的分数 (XP) 更新(上/下)时,我想根据下面的数组测试更新后的分数,以确定他们的排名是否也需要更新。

private $scores = array('3' => '12116', '2' => '15146', '1' => '18932',);

使用更新后的分数,我如何从上述数组中确定相关排名?

一切都开放审查,这是我第一次创建排名系统,所以我希望能做对:)

4

1 回答 1

1

好的,给定这些表:

Ranks
+------+-----------+-------------------------+
| RANK | MIN_SCORE |          NAME           |
+------+-----------+-------------------------+
|  1   |     18932 | Editor-in-Chief         |
|  2   |     15146 | Senior Technical Writer |
|  3   |     12116 | Senior Copywriter       |
+------+-----------+-------------------------+

Members
+-----------+-------+
| MEMBER_ID | SCORE |
+-----------+-------+
|     1     | 17722 |
|     2     | 16257 |
|     3     | 12234 |
+-----+-------------+

您可以使用以下方法获得会员及其等级:

SELECT m.MEMBER_ID, r.NAME
FROM Members m INNER JOIN Ranks r
ON m.SCORE > r.MIN_SCORE -- Pick ranks that a user is eligible for
WHERE MEMBER_ID = ?
HAVING MAX(r.MIN_SCORE) -- Pick the rank with the highest score

这允许您删除整个表,这将使您的代码更易于维护。希望您的排名表会比它总是在内存中的小,但是两者的索引MIN_SCORE可能RANK会有所帮助。

如果您确定此连接是一个性能问题(除非您测量了性能问题,否则请不要这样做),您可以使用类似的查询来更新成员表中的排名,假设它有一个RANK列:

UPDATE Members
SET RANK = (
    SELECT RANK
    FROM Ranks
    WHERE SCORE > MIN_SCORE
    AND MEMBER_ID = MEMBER_ID
    HAVING MAX(MIN_SCORE)
)
于 2012-09-18T21:10:57.600 回答