2009-05-29 28 views
5

我一直在嘗試很多不同的方式來解決這個問題從這個論壇和許多其他人。我似乎無法找到解決這個問題或任何文件,會給我一個直接的答案。MySQL:如何組合多個SELECT查詢,在同一個表上使用不同的WHERE標準?

我想知道你是否可以爲我看看它。

感謝

問題:

我有一個數據庫具有以下表 participant_scores 聯賽 輪

我目前能夠顯示一個單輪的得分,一次一輪......這正是我想要的。 但我也想顯示每個參與者獲得的所有回合的分數。 可以說我們有2輪。我想我的結果屏幕上的輸出是這個樣子:

Currently viewing league 20, Round 1 
of 2: 

User Name | Score | Total Score 

Tom  | 10  | 200 

James  | 50  | 300 

用戶名 - 參與者的姓名 得分=比分爲本輪 總得分=所有輪成績爲這個聯賽加在一起的。

我的Mysql查詢如下。對於它的混亂抱歉,我已經重寫了大約100次,而這種當前的方式是完全可行的唯一方式。

>> league_participants_query(MySQL的)

# FIELDS 
    SELECT  
     participants.participant_id,           # ID - used for functions 
     participants.participant_name,          # NAME 
     participants.participant_gender,          # Participant info    
     classes.class_name,             # Class name 
     schools.school_name,             # School name 
     participant_scores.participant_score,         # Participant score 
     participant_scores.participant_score_id 



    # TABLES 
    FROM   participant_scores, participants, classes, league_schools, schools, leagues, rounds  


    # filter leagues 
    WHERE  leagues.league_id    =  51 

    AND   rounds.league_id    =  51 # the current league we are viewing 
    AND   rounds.round_id     =  25 # the current round of the league we are viewing 

    # filter league schools 
    AND   participant_scores.round_id  =  25 # the current round of the league we are viewing 

    # filter schools allowed 
    AND   league_schools.league_id  =  51 # the current league we are viewing 

    # Filter schools 
    AND   schools.school_id    =  league_schools.school_id 

    # Filter classes 
    AND   classes.school_id    =  schools.school_id           
    AND   classes.year_group_id   =  leagues.year_group_id 

    # Filter participants 
    AND   participants.class_id   =  classes.class_id 

    # Filter participant_scores 
    AND   participant_scores.participant_id =  participants.participant_id 

    #Grouping 
    GROUP BY  participants.participant_id 

回答

7

你想要的這裏是這種形式的子查詢:

SELECT 
    name, 
    round, 
    score, 
    (select sum(score) from scores sc where sc.userid = users.userid) total 
FROM users INNER JOIN scores on users.userid = scores.scoreid 

子查詢的列將每一行來計算你從你的回報初始查詢。

要嘗試將其添加到您的查詢:

SELECT 
    participants.participant_id, 
    participants.participant_name, 
    participants.participant_gender, 
    classes.class_name, 
    schools.school_name, 
    participant_scores.participant_score, 
    participant_scores.participant_score_id, 
    (SELECT sum(participant_score) FROM participant_scores tbl_scores2 
    WHERE tbl_scores2.participant_score_id = participants.participant_id) total 
FROM participant_scores, participants, classes, 
    league_schools, schools, leagues, rounds 
WHERE 
    leagues.league_id = 51 AND 
    rounds.league_id = 51 AND 
    rounds.round_id = 25 AND 
    participant_scores.round_id = 25 AND 
    league_schools.league_id = 51 AND 
    schools.school_id = league_schools.school_id AND 
    classes.school_id = schools.school_id AND 
    classes.year_group_id = leagues.year_group_id AND 
    participants.class_id = classes.class_id AND 
    participant_scores.participant_id = participants.participant_id 
GROUP BY 
    participants.participant_id 

我是有點擔心的子查詢包括多個聯賽,但它看起來像一個參與者只能屬於一個聯盟無論如何。您可能需要在子查詢中包含一些內容來檢查這一點。

+0

你好,謝謝你的深入回覆。我必須讓你知道我是初學者,所有這些對我來說都是試錯。 我正在使用一個程序來幫助我識別代碼上的錯誤,但到目前爲止,我一直無法使您的建議更改使我的腳本運行。我必須輸入他們錯了。 它不會讓我再次在這裏發佈我的代碼。我只剩下240個字符了......我會在下面回答。 謝謝! – Tom 2009-05-29 16:17:37

相關問題