找到联盟中的最佳射手

时间:2013-11-03 04:33:10

标签: php mysql sql database

我是数据库新手。我被赋予了一项与足球联赛有关的任务,其中有些球队在主场和客场比赛,比分存储等等。我有以下表格

goals
======================
goal_id 
goal_player_people_id
goal_game_id
goal_score

player
=====================
player_people_id
player_team_id

people
====================
people_id
people_first_name
people_last_name
people_dob

我需要找出最佳射手的名字,请帮忙。

2 个答案:

答案 0 :(得分:1)

如果goals.goal_player_people_id是对people.people_id的引用:

SELECT p.people_first_name, p.people_last_name, SUM(g.goal_score) totscore
FROM goals g
JOIN people p
ON g.goal_player_people_id = p.people_id
GROUP BY p.people_id
ORDER BY totscore DESC LIMIT 1;

答案 1 :(得分:0)

select  people_first_name people_last_name
from people
where people_id not in(
    select A.id
    from(
        select goal_player_people_id as id,count(*) as gc
        form goals
        group by goal_player_people_id) A,
        (
        select goal_player_people_id as id,count(*) as gc
        form goals
        group by goal_player_people_id) B,
    where A.gc<B.gc
)