创建一个玩家经验大于等级表所需经验的视图
Posted
技术标签:
【中文标题】创建一个玩家经验大于等级表所需经验的视图【英文标题】:Creating a view where player experience is greater than required experience of level table 【发布时间】:2020-02-05 22:06:59 【问题描述】:我一直在尝试创建一个玩家当前级别的视图,通过它所拥有的经验量来显示。我有一个名为“levels”的表和一个名为“characters”的表。这个想法是视图包含玩家的经验大于最低要求数量但也小于下一个查询的级别,因此介于两者之间。
表格字符:
+-----+------------+------------+
| 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`
我通过上面的查询得到的结果是;
我希望任何人都可以帮助我。我已经尝试了很多,但我似乎无法做到正确。提前致谢。
【问题讨论】:
您是否通过此查询获得了以下所有级别? 不。我已经添加了结果。 【参考方案1】:另一种选择是在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 |
【讨论】:
【参考方案2】:考虑以下几点:
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。
【讨论】:
以上是关于创建一个玩家经验大于等级表所需经验的视图的主要内容,如果未能解决你的问题,请参考以下文章