我一直在尝试用玩家当前的水平来创建视图,该视图以其所拥有的经验来显示。我有一个名为“级别”的表和一个名为“字符”的表。想法是,视图包含这样的级别,在该级别中,玩家的体验大于最小所需数量,但也小于下一个查询,因此介于两者之间。
表格字符:
+-----+------------+------------+
| id | name | experience |
+-----+------------+------------+
| 1 | player 1 | 23 |
+-----+------------+------------+
表级别:
+--------+------------+--------------------+
| level | level_name | minimum_experience |
+--------+------------+--------------------+
| 1 | Beginner | 0 |
| 2 | Protector | 20 |
| 3 | Warrior | 40 |
+--------+------------+--------------------+
我要创建的视图是:
+---------------+----------------+------------+-------+----------------------+----------------------+
| character_id | character_name | level_name | level | character_experience | next_level_experience|
+---------------+----------------+------------+-------+----------------------+----------------------+
| 1 | player 1 | Protector | 1 | 23 | 40 |
+---------------+----------------+------------+-------+----------------------+----------------------+
我现在用于视图的查询之一是,但这不起作用。
SELECT
`experiment`.`characters`.`character_id` AS `character_id`,
`experiment`.`characters`.`character_name` AS `character_name`,
`experiment`.`characters`.`experience` AS `current_experience`,
`experiment`.`levels`.`level` AS `current_level`,
`experiment`.`levels`.`level_name` AS `level_name`,
`experiment`.`levels`.`experience` AS `next_levelexp`
FROM
(
`experiment`.`characters`
LEFT JOIN `experiment`.`levels` ON
(
(
`experiment`.`levels`.`experience` < `experiment`.`characters`.`experience`
)
)
)
GROUP BY
`experiment`.`characters`.`character_id`
我希望任何人都能帮助我。我已经尝试了很多,但似乎无法正确完成。预先感谢。
请考虑以下内容:
DROP TABLE IF EXISTS characters;
CREATE TABLE characters
(id SERIAL PRIMARY KEY
,name VARCHAR(12) UNIQUE
,experience INT NOT NULL
);
INSERT INTO characters VALUES
(1,'player 1',23);
DROP TABLE IF EXISTS levels;
CREATE TABLE levels
(level SERIAL PRIMARY KEY
,level_name VARCHAR(12) UNIQUE
,minimum_experience INT NOT NULL
);
INSERT INTO levels VALUES
(1,'Beginner',0),
(2,'Protector',20),
(3,'Warrior',40);
SELECT c.*
, MAX(x.minimum_experience) minimum_experience
, MIN(y.minimum_experience) next_level_exp
FROM characters c
JOIN levels x
ON x.minimum_experience <= c.experience
LEFT
JOIN levels y
ON y.minimum_experience > c.experience
GROUP
BY c.id;
+----+----------+------------+--------------------+----------------+
| id | name | experience | minimum_experience | next_level_exp |
+----+----------+------------+--------------------+----------------+
| 1 | player 1 | 23 | 20 | 40 |
+----+----------+------------+--------------------+----------------+
我为读者做一点练习。提示:它涉及另一个JOIN。
另一个选择是在SELECT
子句(View on DB Fiddle)内使用相关子查询:
查询
SELECT c.*
, (SELECT level_name
FROM levels
WHERE minimum_experience <= c.experience
ORDER BY minimum_experience DESC
LIMIT 1) AS level_name
, (SELECT level
FROM levels
WHERE minimum_experience <= c.experience
ORDER BY minimum_experience DESC
LIMIT 1) AS level
, (SELECT minimum_experience
FROM levels
WHERE minimum_experience > c.experience
ORDER BY minimum_experience ASC
LIMIT 1) AS next_levelexp
FROM characters c;
结果
| id | name | experience | level_name | level | next_levelexp |
| --- | -------- | ---------- | ---------- | ----- | ------------- |
| 1 | player 1 | 23 | Protector | 2 | 40 |