简体   繁体   English

PHP / SQL-获取另一列的总和

[英]PHP / SQL - Get sum of another column

I have two tables in my MySQL database. 我的MySQL数据库中有两个表。 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 . 因此,例如,我们可以看到球员JohnSam拥有世界Antica Therefore, Antica should have the value "2" under the player column in the table world. 因此,Antica在表世界的玩家列下应具有值“ 2”。 But only John is online on Antica. 但是只有约翰在Antica上在线。 So the world Antica should have the value "1" under the column "online" in the table "world". 因此,世界“ Antica”表“ world”中的“ online”列下的值应为“ 1”。 And so on. 等等。 I also want their average level on that world. 我也希望他们在这个世界上的平均水平。

How can I achieve this with PHP by making an SQL query? 如何通过PHP进行SQL查询来实现此目的?

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.. 然后使用这些值更新世界表。

You can summarize the data as: 您可以将数据汇总为:

select world, count(*) as players, sum(online) as online, avg(level) as avglevel
from players
group by world;

You can incorporate this into an update : 您可以将其合并到update

update worlds w join
       (select world, count(*) as players, sum(online) as online, avg(level) as avglevel
        from players
        group by world
       ) p
       on w.world = p.world
    set w.players = p.player,
        w.online = p.online,
        w.avglevel = p.avglevel;

I don't think maintaining a separate table like this is a good idea. 我认为这样维护一个单独的表不是一个好主意。 After all, you can always just query players to get this information. 毕竟,您始终可以查询players以获取此信息。 Also the two systems can quickly get out-of-date, so the data is inconsistent. 同样,这两个系统也可能很快过时,因此数据不一致。 You would fix that by using triggers, but that adds complexity to the system. 您可以使用触发器来解决此问题,但这会增加系统的复杂性。 In general, a simple query is quite sufficient. 通常,一个简单的查询就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM