MySQL数据库,选择大小写,并获取两个值,而不是一个

时间:2016-12-09 21:59:37

标签: mysql sql database

如果我想了解a_player1_name和b_player1_name,我怎么能修改这个?

现在返回将找到的第一个值。我的意思是,a_player1_name或b_player1_name,但不是两者。两者都是必需的。

当chart_matches.a_player1_id = user_list.id时,则a_player1_name应为user_list.player_name。

当chart_matches.b_player1_id = user_list.id时,则b_player1_name应为user_list.player_name。

请帮助,谢谢。

SELECT case user_list.id
            when chart_matches.a_player1_id
            then user_list.player_name
       end AS a_player1_name

       , case user_list.id
            when chart_matches.b_player1_id
            then user_list.player_name
       end AS b_player1_name

FROM chart_matches
     , all_lists
     , user_list

WHERE chart_matches.chart_id = 106
      AND ((chart_matches.a_team_id = all_lists.id AND chart_matches.a_player1_id = user_list.id)
      OR (chart_matches.b_team_id = all_lists.id AND chart_matches.b_player1_id = user_list.id))

1 个答案:

答案 0 :(得分:0)

看起来您只想聚合您的结果(请注意以下版本中的MAX()函数。)

你的原作将获得2 条记录 - 每个玩家一个。因此,您必须将记录聚合在一起,并且对于此MAX()基本上保留记录中与特定播放器匹配的值。

SELECT max(case user_list.id
            when chart_matches.a_player1_id
            then user_list.player_name
            else null
       end) AS a_player1_name

       , max(case user_list.id
            when chart_matches.b_player1_id
            then user_list.player_name
            else null
       end) AS b_player1_name

FROM chart_matches
     , all_lists
     , user_list

WHERE chart_matches.chart_id = 106
      AND chart_matches.a_team_id = all_lists.id
      AND (chart_matches.a_player1_id = user_list.id OR chart_matches.b_player1_id = user_list.id)