Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 是否可以将联接添加到此SQL语句?_Mysql_Join - Fatal编程技术网

Mysql 是否可以将联接添加到此SQL语句?

Mysql 是否可以将联接添加到此SQL语句?,mysql,join,Mysql,Join,我有一个表和一个查询,用来返回进球最多的球员名单 查询非常有效,但是为了获得用户名,我必须根据返回的结果(player_id)执行另一个查询 是否可以修改我现有的查询以连接两个表?我知道这在普通查询中是可能的,我只是不确定它是否在这个实例中,因为将返回自定义结果表 这是我的初始表,名为结果: +----------------+-------------+-----+ | Field | Type | Key | +----------------+-------

我有一个表和一个查询,用来返回进球最多的球员名单

查询非常有效,但是为了获得用户名,我必须根据返回的结果(player_id)执行另一个查询

是否可以修改我现有的查询以连接两个表?我知道这在普通查询中是可能的,我只是不确定它是否在这个实例中,因为将返回自定义结果表

这是我的初始表,名为
结果

+----------------+-------------+-----+
| Field          | Type        | Key |
+----------------+-------------+-----+
| results_id     | int         | Pri |
| community_id   | int         |     |
| player1_id     | int         |     |
| player1_goals  | int         |     |
| player2_id     | int         |     |
| player2_goals  | int         |     |
+----------------+-------------+-----+
这是我用来返回结果的查询:

select player, sum(goals) from 
((select player1_id as player, player1_goals as goals from results where community_id = 5 ) 
union all 
(select player2_id as player, player2_goals as goals from results where community_id = 5 ) 
) p 
group by player 
order by sum(goals) desc
我的结果就是这样返回的:

+----------------+------------+
| Player         | sum(goals) |
+----------------+------------+
| 2              | 94         |
| 14             | 63         |
| 7              | 43         |
+----------------+------------+
是否可以修改上述查询并将
join
添加到我的
users
表中:

+--------+-----------+
| id     | user_name  |
+---------------------+
| 2      | John       |
| 7      | Andrew     |
| 14     | Charles    |
+--------+------------+
要获得以下内容的输出:

+----------------+----------------+------------+
|user_name       | Player         | sum(goals) |
+----------------+----------------+------------+
| John           | 2              | 94         |
| Charles        | 14             | 63         |
| Andrew         | 7              | 43         |
+----------------+----------------+------------+

简短回答-是的,您可以:

select u.user_name, sum(goals) from 
((select player1_id as player, player1_goals as goals from results where community_id = 5 ) 
union all 
(select player2_id as player, player2_goals as goals from results where community_id = 5 ) 
) p 
join users u on p.player = u.id
group by player 
order by sum(goals) desc

您可以通过加入
来完成。您也可以表示为:

select u.*,
       (select sum(case when u.id = r.player1_id then r.player1_goals else r.player2_goals)
        from results r
        where r.community_id = 5 and
              u.id in (r.player1_id, r.player2_id)
       ) as goals
from users u;

杰出的成功了!这就是我喜欢的一个长问题的简短答案!:)一场比赛只有两个人吗?不是每个球员都应该有一排球员表吗?