简体   繁体   English

如何在具有大于条件的 Select 语句的 mySQL 中使用 OR?

[英]How do I use OR in mySQL on a Select statement with a greater-than condition?

So I am basically building this mySQL sports database and have a table in which there is a 'Home_Team' and 'Away_Team', as well as 'Home_Score' and 'Away_Score'.所以我基本上是在构建这个 mySQL 体育数据库,并有一个表,其中有一个“Home_Team”和“Away_Team”,以及“Home_Score”和“Away_Score”。 What I need is a query which displays the winning team and score.我需要的是一个显示获胜球队和得分的查询。 So it has to select one team if the score is greater than the other team's;因此,如果分数大于另一队,则必须 select 一支队; and vice-versa.反之亦然。 I have tried the following, using an OR statement between the sub-queries:我尝试了以下方法,在子查询之间使用 OR 语句:

SELECT *
FROM match
WHERE (SELECT Home_Team FROM match WHERE Home_Score > Away_Score) 
OR (SELECT Away_Team FROM match WHERE Home_Score < Away_Score);

This doesn't run since the subquery returns more than one row.这不会运行,因为子查询返回多行。 Any help is much appreciated:)任何帮助深表感谢:)

Every time any of the team must have won or match tied so you can use the case expression in the SELECT clause itself每次必须有任何球队获胜或平局,因此您可以在SELECT子句本身中使用case表达式

SELECT case when Home_Score > Away_Score then Home_Team 
            when Home_Score < Away_Score then Away_Team
            else 'tie'
       end as winner_team,
       greatest(Home_Score ,Away_Score ) as winner_score
FROM match

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM