0

我有一张桌子叫accounts,另一张叫level_points

基本上,这个想法是确定你需要达到 X 级的最低点数是多少。

账户结构

id, name.. 等points

Level_Points 结构

level,points

这里的值,例如

(1, 5)
(2, 10)
(3, 15)

我可以使用此查询计算级别

SELECT `level` 
FROM `level_points` 
WHERE `points` <= (SELECT `points` 
                   FROM `accounts` 
                   WHERE `id` = 'x') 
ORDER BY `level` 
DESC LIMIT 1

我的问题是,现在我正在尝试加入表格以获得类似的东西(对于帐户表中的每个用户)

结果:

对于用户 1:`id`、`name` 等...`points`、`level`
对于用户 2:`id`、`name` 等...`points`、`level`
对于用户 3:`id`、`name` 等...`points`、`level`

我不确定如何使用连接来做到这一点,而且我似乎无法在这里找到对我有帮助的答案。

4

3 回答 3

1

尝试这样的事情:

SELECT ac.id,
    ac.name,
    ac.points,
    le.level
FROM account ac,
    level le
WHERE ac.id = 'x'
    AND le.level= (
        SELECT level
        FROM level_points
        WHERE points <= (
                SELECT points
                FROM accounts
                WHERE id = 'x'
                )
        ORDER BY level DESC LIMIT 1
        );

要为所有玩家获取它,您可以执行以下操作:

SELECT ac.id,
    ac.name,
    ac.points,
    le.level
FROM accounts ac,
    level_points le
WHERE le.level= (
        SELECT level
        FROM level_points
        WHERE points <= (
                SELECT points
                FROM accounts ac2
                WHERE ac2.id = ac.id
                )
        ORDER BY level DESC LIMIT 1
        );

检查这个小提琴看看它工作

于 2013-09-15T10:55:47.580 回答
0

我会这样改变Level_Points表格:

ALTER TABLE Level_Points
ADD max_points INT NOT NULL;

ALTER TABLE Level_Points
ADD CONSTRAINT CK_LevelPoints_VerifyPoints CHECK(points < max_points);

在这种情况下,points列将存储最小数量。点数和max_points列数将存储最大数量。当前级别所需的点数。接下来,我必须确保这个级别是连续的,它们没有重叠并且没有间隙(但这些限制超出了此答案的目的:请参阅Alexander Kuznetsov 撰写的这篇文章)。此外,还需要一些索引:

CREATE UNIQUE INDEX IUN_LevelPoints_points
ON Level_Points(points);

CREATE UNIQUE INDEX IUN_LevelPoints_points_maxpoints
ON Level_Points(points, max_points); -- This index is useful because the final query needs both columns: points and max_points; in SQL Server, max_points column can be covered column: ... ON Level_Points(points) INCLUDE (max_points), then the first index (IUN_LevelPoints_points) is no more needed

存储在此表中的一些示例行:

INSERT INTO Level_Points ("level", "points", "max_points") VALUES (0, 0,  5); -- level 0 for players which have less than 5 points
INSERT INTO Level_Points ("level", "points", "max_points") VALUES (1, 5,  10);
INSERT INTO Level_Points ("level", "points", "max_points") VALUES (2, 10, 15);
INSERT INTO Level_Points ("level", "points", "max_points") VALUES (3, 15, 100);
INSERT INTO Level_Points ("level", "points", "max_points") VALUES (4, 100,2147483647); -- 2147483647 is the maximum value for a INT in SQL Server

在这种情况下,最终查询只是一个简单的左/内连接:

SELECT ..., lvl.level
FROM accounts AS acc LEFT JOIN Level_Points AS lvl 
ON acc.points >= lvl.points 
AND acc.points < lvl.max_points
于 2013-09-15T11:36:45.487 回答
0

尝试这个 ..

       select column1 from table1 
       inner join table2 on table1.column = table2.column
       where table2.column=0

看看这个

http://www.w3schools.com/sql/sql_join.asp

于 2013-09-15T10:50:07.867 回答