这个SQL查询出了什么问题?ERROR 1054(42S22)

时间:2017-10-14 23:23:42

标签: mysql sql

错误1054(42S22):'where子句'中的未知列'm.match_id'

(select m.match_id,m.team1,m.team2,m.team1_score from match_results as m where m.team1_score=( select max(score) from ( select p.team1_score as score from match_results p where p.match_id=m.match_id UNION select q.team2_score as score from match_results q where q.match_id=m.match_id ) as T )) UNION (select m1.match_id,m1.team1,m1.team2,m1.team2_score from match_results m1 where m1.team2_score=( select max(score) from ( select team1_score as score from match_results where match_id=m1.match_id UNION select team2_score as score from match_results where match_id=m1.match_id )as T ));

架构:match_results(match_id,team1,team2,team1_score,team2_score) team1和team2是varchar() team1_score和team2_score是整数。 我正在尝试以最高分(team1_score或team2_score)获取团队(team1或team2))

2 个答案:

答案 0 :(得分:2)

我简化了您的查询

select m.team1,m.team2,m.team1_score 
   from match_results as m,
        match_results as p
  where m.team1_score= greatest (p.team1_score, p.team2_score)
    AND m.match_id = p.match_id

UNION

select m.team1,m.team2,m.team2_score 
   from match_results as m,
        match_results as p
  where m.team2_score= greatest (p.team1_score, p.team2_score)
    AND m.match_id = p.match_id

你会不会重复同一次match_id? (逻辑上没有!)如果是这样,请使用上面的查询,否则你甚至可以使用简单的

如果team1的分数最高,此查询将给出team1的分数。如果team2得分高于team1

,它将给予team2的分数
 select m.team1, m.team2, greatest (m.team1_score, m.team2_score)
       from match_results as m

答案 1 :(得分:0)

  

ERROR 1054(42S22)

此错误表示您的某个字段名称错误 你要别名它并使用错误的别名,或者只是错误的字段名称 另外,为什么查询中有两个AS t