简体   繁体   English

Php Mysql 总和超过 2 列和 select 来自 2 个表

[英]Php Mysql sum over 2 colums and select from 2 tables

I have a MySQL database with 3 tables.我有一个包含 3 个表的 MySQL 数据库。

  • Table player桌上播放器

P_id = content (1, 2, 3,)

P_name = content (Spieler1name, Spieler2name, Spieler3name)
  • Table tournament桌上锦标赛

T_id = content (1, 2)

T_date = content (2018, 2019)

T_players = content (123, 123)
  • Table Games桌上游戏

G_id = content (1, 2)

G_tournament_id = content (T_id from table tournament)

G_player_away = Content (as an example user id 1)

G_player_home = content (as an example user id 2)

G_player_away_points = Content (11)

G_player_home_points = Content (8)

The Games table has even more data. Games 表包含更多数据。

Now I want to create a table with PhP as ranking.现在我想创建一个以 PhP 作为排名的表。

Place name points地名点

  1. Spieler1name 11斯皮勒名字 11

  2. Spieler2name 8斯皮勒名字 8

  3. Spieler3name 4斯皮勒3名4

  4. .... ……

Can someone tell me how the php query from the mysql database works?有人能告诉我 mysql 数据库中的 php 查询是如何工作的吗? I tested this but it doesn't work.我对此进行了测试,但它不起作用。

SELECT SUM(G_player_away_points) AS total FROM Games 
UNION
SELECT SUM(G_player_home_points) AS total FROM Games
GROUP BY G_player_away

In the code above, he also sums the points from the opponent.在上面的代码中,他还总结了对手的分数。 This is of course not correct, it should only the points obtained by him appear in the output with php table.这当然是不正确的,应该只有他得到的分数出现在 output 和 php 表中。 Furthermore you have to get the name from the table players.此外,您必须从桌面玩家那里获得名称。 Can someone help me?有人能帮我吗?

If you are using single table and multiple column for sum then you should try with this如果您使用单表和多列求和,那么您应该尝试使用这个

SELECT SUM(G_player_away_points + G_player_home_points) AS total FROM Games 
GROUP BY G_player_away

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

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