每个派生表必须有自己的别名mysql错误消息

时间:2017-02-18 13:02:38

标签: mysql subquery left-join alias

我有以下查询,但我不确定别名需要在哪里可以任何人帮助防止我得到的错误

//错误

Every derived table must have its own alias

//查询

SELECT user_id, 
       SUM(win) as won, SUM(draw) As drawn, SUM(loss) as lost, 
       SUM(goals_for) as goals_for, SUM(goals_against) as goals_against, 
       SUM(goals_for) - SUM(goals_against) as goals_difference, 
       player, username, community_result_id, count(*) as played, 
       SUM(points) as points     
FROM 
    ( SELECT home_team_user_id as user_id, 
      CASE WHEN home_score > away_score THEN 1 ELSE 0 END as win, 
      CASE WHEN home_score = away_score THEN 1 ELSE 0 END as draw, CASE 
      WHEN home_score < away_score THEN 1 ELSE 0 END as loss,
      CASE WHEN home_score > away_score THEN 3 
  WHEN home_score = away_score THEN 1 ELSE 0 END points,
       home_score as goals_for, away_score as goals_against,
       users.name as player, users.username, 
       community_results.community_result_id FROM community_results 
     LEFT JOIN users ON community_results.home_team_user_id=users.id 
     UNION ALL SELECT away_team_user_id as user_id, 
     CASE WHEN away_score > home_score THEN 1 ELSE 0 END as win, 
     CASE WHEN away_score = home_score THEN 1 ELSE 0 END as draw, 
     CASE WHEN away_score < home_score THEN 1 ELSE 0 END as loss, 
     CASE WHEN home_score > away_score THEN 3 WHEN home_score = away_score 
     THEN 1 ELSE 0 END points, away_score as goals_for, home_score as goals_against, 
     users.name as player, users.username, 
     community_results.community_result_id FROM community_results 
     LEFT JOIN users ON community_results.away_team_user_id = users.id ) 
     WHERE user_id = 1 GROUP BY user_id ORDER By won desc LIMIT 1

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT user_id, 
       SUM(win) as won, SUM(draw) As drawn, SUM(loss) as lost, 
       SUM(goals_for) as goals_for, SUM(goals_against) as goals_against, 
       SUM(goals_for) - SUM(goals_against) as goals_difference, 
       player, username, community_result_id, count(*) as played, 
       SUM(points) as points     
FROM 
    ( SELECT home_team_user_id as user_id, 
      CASE WHEN home_score > away_score THEN 1 ELSE 0 END as win, 
      CASE WHEN home_score = away_score THEN 1 ELSE 0 END as draw, CASE 
      WHEN home_score < away_score THEN 1 ELSE 0 END as loss,
      CASE WHEN home_score > away_score THEN 3 
  WHEN home_score = away_score THEN 1 ELSE 0 END points,
       home_score as goals_for, away_score as goals_against,
       users.name as player, users.username, 
       community_results.community_result_id FROM community_results 
     LEFT JOIN users ON community_results.home_team_user_id=users.id 
     UNION ALL SELECT away_team_user_id as user_id, 
     CASE WHEN away_score > home_score THEN 1 ELSE 0 END as win, 
     CASE WHEN away_score = home_score THEN 1 ELSE 0 END as draw, 
     CASE WHEN away_score < home_score THEN 1 ELSE 0 END as loss, 
     CASE WHEN home_score > away_score THEN 3 WHEN home_score = away_score 
     THEN 1 ELSE 0 END points, away_score as goals_for, home_score as goals_against, 
     users.name as player, users.username, 
     community_results.community_result_id FROM community_results 
     LEFT JOIN users ON community_results.away_team_user_id = users.id ) t
     WHERE user_id = 1 GROUP BY user_id ORDER By won desc LIMIT 1