2013-11-03 64 views
0

我是新來的數據庫。我一直在考慮一個賦值,這是值得做的足球聯賽,其中一些球隊打主客場和得分存儲等等等等,我有以下表尋找聯賽最佳射手

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 

我需要找出的名字最佳射手,請幫助。

+0

你的表真名叫那麼久嗎? –

+0

它是列名,而不是表名。 –

+2

這是一個非常簡單的查詢。你有什麼嘗試? – syrion

回答

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 
) 
1

如果goals.goal_player_people_idpeople.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;