创建玩家体验大于级别 table 所需体验的视图
Creating a view where player experience is greater than required experience of level table
我一直在尝试创建一个视图,其中玩家的当前等级由其拥有的经验量来显示。我有一个名为 "levels" 的 table 和一个名为 "characters" 的 table。这个想法是视图包含玩家经验大于最低要求量但也小于下一个查询的级别,因此介于两者之间。
第table个字符:
+-----+------------+------------+
| id | name | experience |
+-----+------------+------------+
| 1 | player 1 | 23 |
+-----+------------+------------+
table 级别:
+--------+------------+--------------------+
| 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 |
+----+----------+------------+--------------------+----------------+
我遗漏了一点作为 reader 的练习。提示:它涉及另一个 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 |
我一直在尝试创建一个视图,其中玩家的当前等级由其拥有的经验量来显示。我有一个名为 "levels" 的 table 和一个名为 "characters" 的 table。这个想法是视图包含玩家经验大于最低要求量但也小于下一个查询的级别,因此介于两者之间。
第table个字符:
+-----+------------+------------+
| id | name | experience |
+-----+------------+------------+
| 1 | player 1 | 23 |
+-----+------------+------------+
table 级别:
+--------+------------+--------------------+
| 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 |
+----+----------+------------+--------------------+----------------+
我遗漏了一点作为 reader 的练习。提示:它涉及另一个 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 |