错误 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/