나는 가장 많은 골을 넣은 플레이어 목록을 반환하는 데 사용하는 테이블과 쿼리를 가지고 있습니다.이 SQL 문에 JOIN을 추가 할 수 있습니까?
쿼리가 잘 작동하지만 사용자 이름을 가져 오기 위해 반환 된 결과 (player_id)를 기반으로 다른 쿼리를 수행해야합니다.
기존 쿼리를 수정하여 두 테이블을 조인 할 수 있습니까? 나는 이것이 정상적인 쿼리로 가능하다는 것을 안다. 반환되는 커스텀 결과 테이블 때문에이 인스턴스에 있는지는 확실하지 않다.
이 호출 내 초기 테이블, results
입니다 : 이것은 내 결과를 반환하기 위해 사용하고있는 쿼리입니다
+----------------+-------------+-----+
| 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 |
+----------------+------------+
위의 쿼리를 수정하고을 추가 할 수 있습니까? 내 users
테이블:
+--------+-----------+
| id | user_name |
+---------------------+
| 2 | John |
| 7 | Andrew |
| 14 | Charles |
+--------+------------+
이의 출력으로 활용하려면 다음
+----------------+----------------+------------+
|user_name | Player | sum(goals) |
+----------------+----------------+------------+
| John | 2 | 94 |
| Charles | 14 | 63 |
| Andrew | 7 | 43 |
+----------------+----------------+------------+
게임에는 2 명의 플레이어 만 있습니까? 각 플레이어가 Players 테이블을 열어야할까요? –