简体   繁体   English

sql查询未返回正确结果

[英]sql query not returning correct results

Ive got a table containing results for a certain sport competition from 2009 - present. Ive得到了一张表格,其中包含2009年至今的某项体育比赛的结果。 The table contains numerous columns such as hometeam, awayteam, homescore, awayscore etc. As you can see from the image below: 该表包含许多列,例如hometeam,awayteam,homescore,awayscore等。从下图可以看到:

在此处输入图片说明

If I want to display results for a certain team (in this example team Sharks) for both home and away matches I do it like so: 如果我想显示某支球队(本例中为“鲨鱼队”) 的主场和客场比赛结果,我会这样做:

    SELECT *
FROM `results`
WHERE `hometeam` = 'Sharks' || `awayteam` = 'Sharks'

The above code is working fine and I get the correct results as you can see from the image: 上面的代码工作正常,从图像中可以看到正确的结果:

在此处输入图片说明

The problem 问题

The problem im having is when I want to display BOTH home and away match results for only two SPECIFIC teams, im not getting the correct results. 问题是当我只想显示两个特定球队的主场和客场比赛结果时,我没有得到正确的结果。 (In the query below im trying to only display home and away results when team Stormers played against team Sharks) I query the table as follows: (在下面的查询中,im仅试图在Stormers队与Sharks队对垒时显示本垒打和离队结果),我查询该表如下:

SELECT *
FROM `results`
WHERE `hometeam` = 'Stormers' || `awayteam` = 'Stormers' && `hometeam` = 'Sharks' || `awayteam` = 'Sharks'

You can see from the image below that the above query is returning the wrong results. 您可以从下图看到上述查询返回错误的结果。 The query is returning results for both team Stormers and team Sharks for ALL their home and away matches against ALL opponents 该查询返回的是Stormer团队和Sharks团队针对所有对手的所有本场比赛和客场比赛的结果

在此处输入图片说明

The question 问题

Im looking for a way where the query will only return results for specific teams when they played against each other home and away. 我正在寻找一种查询方法,该查询仅在特定团队在主场与客场对战时返回结果。 Example: for this query I only wanted to display all results for team Stormers against team Sharks, ignoring all other opponents. 示例:对于该查询,我只想显示“暴风队”对鲨鱼队的所有结果,而忽略所有其他对手。

If anyone can be so kind to point me in the right direction it would be greatly appreciated. 如果有人能这样向我指出正确的方向,将不胜感激。 Thank you in advance. 先感谢您。

I looks like you just have wrong logic. 我看起来您逻辑错误。 Try this: 尝试这个:

SELECT *
FROM `results`
WHERE (`hometeam` = 'Stormers' AND `awayteam` = 'Sharks')
OR (`hometeam` = 'Sharks' AND `awayteam` = 'Stormers')

You could do it like this: 您可以这样做:

SELECT *
FROM `results`
WHERE `hometeam` IN ('Stormers', 'Sharks')
AND `awayteam` IN ('Stormers', 'Sharks')

This should only return matches where those two teams played against each other. 这只应返回两支球队互相对抗的比赛。

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

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