I’ve been trying to create a view with the player’s current level is shown by the amount of experience it has. I have a table called “levels” and a table called “characters”. The idea is that the view contains the level where the player’s experience is greater than the minimum required amount but also less than the next query, so in between.
The table characters:
+-----+------------+------------+
| id | name | experience |
+-----+------------+------------+
| 1 | player 1 | 23 |
+-----+------------+------------+
The table levels:
+--------+------------+--------------------+
| level | level_name | minimum_experience |
+--------+------------+--------------------+
| 1 | Beginner | 0 |
| 2 | Protector | 20 |
| 3 | Warrior | 40 |
+--------+------------+--------------------+
Where as the view I want to create is:
+---------------+----------------+------------+-------+----------------------+----------------------+
| character_id | character_name | level_name | level | character_experience | next_level_experience|
+---------------+----------------+------------+-------+----------------------+----------------------+
| 1 | player 1 | Protector | 1 | 23 | 40 |
+---------------+----------------+------------+-------+----------------------+----------------------+
One of the queries I use for the view now is, but that doesn’t work.
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`
The results I get with the query above are;
I hope anyone can help me. I’ve tried a lot but I can’t seem to get it right. Thanks in advance.
2
Answers
Consider the following:
I’ve left out one bit as an exercise for the reader. Hint: it involves another JOIN.
Another Option is to use Correlated Subquery inside the
SELECT
clause (View on DB Fiddle):Query
Result