mysql - 这个sql查询有什么问题?ERROR 1054 (42S22)

标签 mysql sql

错误 1054 (42S22):“where 子句”中存在未知列“m.match_id”

(从 match_results 中选择 m.match_id,m.team1,m.team2,m.team1_score 作为 m 在哪里 m.team1_score=( 从 ( 中选择最大值(分数) 选择 p.team1_score 作为 match_results p 的得分,其中 p.match_id=m.match_id 联盟 选择 q.team2_score 作为 match_results q 的分数,其中 q.match_id=m.match_id ) 作为 T )) 联盟 (从 match_results m1 中选择 m1.match_id,m1.team1,m1.team2,m1.team2_score 在哪里 m1.team2_score=( 从 ( 中选择最大值(分数) 从 match_results 中选择 team1_score 作为分数,其中 match_id=m1.match_id 联盟 从 match_results 中选择 team2_score 作为分数,其中 match_id=m1.match_id ) 作为 T ));

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

最佳答案

我已经简化了您的查询

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

关于mysql - 这个sql查询有什么问题?ERROR 1054 (42S22),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46750258/

相关文章:

sql - 是否有针对 SQL 查询的单元测试,以衡量它们在架构更改后是否同时运行?

mysql - 复杂的 MySQL Select 查询

mysql - 如何删除 MySQL 表中的每一列?

MySQL - 从列中的两行返回多个条目的最新日期和总和

sql - 在没有游标的 SQL 中展平 SQL 分层数据结构

sql - 如何处理postgresql函数中的单引号

mysql - 如何在phpMyAdmin中使用自动增量

mysql - 这个 UPDATE 查询有什么问题?

mysql - 是否可以使用 SELECT * from TableName 显示文本而不是 ID

sql - 如何从 CodeIgniter 中具有相同列名的表中输出数据?