MySQL:如何在同一个表上使用不同的WHERE标准组合多个SELECT查询?

时间:2009-05-29 13:17:50

标签: mysql

我一直在尝试从这个论坛和其他许多方面解决这个问题的许多不同方法。我似乎无法找到解决这个问题的方法或任何能给我直接答案的文档。

我想知道你是否可以帮我看一下。

由于

问题:

我有一个包含以下表格的数据库 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

1 个答案:

答案 0 :(得分: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

我有点担心包含多个联赛的子查询,但看起来单个参与者无论如何都只能属于一个联盟。您可能需要在子查询中包含一些内容来检查这一点。