I have two tables in my MySQL database. One is called players and the other table is called worlds. Each player has a "level" and a "world" column. What I want to achieve is to fill up the columns in the table world based on the column values from table players. I want to first of all get the sum of players on that world and also an average of their levels.
My tables look like this:
Table: Players
+-------+-------+--------+----------+-------+--------------+---------+--------+
| id | name | sex | vocation | level | achievements | world | online |
+-------+-------+--------+----------+-------+--------------+---------+--------+
| 24471 | John | Male | None | 92 | 3 | Antica | 1 |
| 24493 | Bob | Male | None | 76 | 19 | Amera | 0 |
| 24535 | Sam | Male | None | 75 | 0 | Antica | 0 |
| 24574 | Sarah | Female | None | 78 | 23 | Beneva | 1 |
| 24673 | Carl | Male | None | 75 | 10 | Belobra | 1 |
+-------+-------+--------+----------+-------+--------------+---------+--------+
Table: Worlds
+----+---------+---------+--------+--------+----------+---------------+--------------+
| id | name | players | online | avglvl | totalexp | location | pvp |
+----+---------+---------+--------+--------+----------+---------------+--------------+
| 1 | Amera | 0 | 0 | 0 | 0 | North America | Open PvP |
| 2 | Antica | 0 | 0 | 0 | 0 | Europe | Open PvP |
| 3 | Astera | 0 | 0 | 0 | 0 | North America | Optional PvP |
| 4 | Belobra | 0 | 0 | 0 | 0 | South America | Optional PvP |
| 5 | Beneva | 0 | 0 | 0 | 0 | Europe | Optional PvP |
+----+---------+---------+--------+--------+----------+---------------+--------------+
So for example, we can see that the players John and Sam have the world Antica. Therefore, Antica should have the value "2" under the player column in the table world. But only John is online on Antica. So the world Antica should have the value "1" under the column "online" in the table "world". And so on. I also want their average level on that world.
How can I achieve this with PHP by making an SQL query?
First I need to get all the world names (I think?) and loop through them. And then get the following details for each world:
Player amount
Online amount
Average level amount
And then make an update in to worlds table with those values..