为什么具有2个条件的A WHERE子句中的OR语句不运行?

huangapple go评论59阅读模式
英文:

Why does this OR Statement in A WHERE clause that has 2 conditions not run

问题

以下是翻译好的部分:

查询结果集列表为空。在WHERE子句中使用AND创建更复杂的条件如下:

选择
日期,

  • 将主队标识为巴塞罗那或皇家马德里
    CASE WHEN hometeam_id = 8634 THEN 'FC Barcelona'
    ELSE 'Real Madrid CF' END AS home,
  • 将客队标识为巴塞罗那或皇家马德里
    CASE WHEN awayteam_id = 8634 THEN 'FC Barcelona'
    ELSE 'Real Madrid CF' END AS away
    从matches_spain
    WHERE (awayteam_id = 8634 AND hometeam_id = 8634)
    OR (awayteam_id = 8633 AND hometeam_id = 8633);

这是表的架构

matches_spain
id
country_id
season
stage
日期
hometeam_id
awayteam_id
主场进球
客场进球

然而,这个查询有效。

选择
日期,

  • 将主队标识为巴塞罗那或皇家马德里
    CASE WHEN hometeam_id = 8634 THEN 'FC Barcelona'
    ELSE 'Real Madrid CF' END AS home,
  • 将客队标识为巴塞罗那或皇家马德里
    CASE WHEN awayteam_id = 8634 THEN 'FC Barcelona'
    ELSE 'Real Madrid CF' END AS away
    从matches_spain
    WHERE (awayteam_id = 8634 OR hometeam_id = 8634)
    AND (awayteam_id = 8633 OR hometeam_id = 8633);
英文:

The result set list on the below query is empty. Using AND to create a more complex condition within there WHERE clause

SELECT 
	date,
	-- Identify the home team as Barcelona or Real Madrid
	CASE WHEN hometeam_id = 8634 THEN 'FC Barcelona' 
         ELSE 'Real Madrid CF' END AS home,
    -- Identify the away team as Barcelona or Real Madrid
	CASE WHEN awayteam_id = 8634 THEN 'FC Barcelona' 
         ELSE 'Real Madrid CF' END AS away
FROM matches_spain
WHERE (awayteam_id = 8634 AND hometeam_id = 8634)
      OR (awayteam_id = 8633 AND hometeam_id = 8633);

This is the schema of the table

matches_spain
id
country_id
season
stage
date
hometeam_id
awayteam_id
home_goal
away_goal

Yet this works.

SELECT 
	date,
	-- Identify the home team as Barcelona or Real Madrid
	CASE WHEN hometeam_id = 8634 THEN 'FC Barcelona' 
         ELSE 'Real Madrid CF' END AS home,
    -- Identify the away team as Barcelona or Real Madrid
	CASE WHEN awayteam_id = 8634 THEN 'FC Barcelona' 
         ELSE 'Real Madrid CF' END AS away
FROM matches_spain
WHERE (awayteam_id = 8634 OR hometeam_id = 8634)
      AND (awayteam_id = 8633 OR hometeam_id = 8633);

答案1

得分: 1

请注意,WHERE 子句在概念上测试每一行数据是否满足条件。

条件awayteam_id = 8634 AND hometeam_id = 8634只对awayteam_idhometeam_id都等于8634的行为真。

用示例来可视化:

awayteam_id hometeam_id awayteam_id = 8634 AND hometeam_id = 8634
8001 8002 false
8001 8634 false
8634 8002 false
8634 8634 true

根据列名,我猜测这个表代表两支队伍之间的体育比赛。如果是这样的话,最后一行代表一支队伍与自己比赛。

如果你没有这样的比赛记录,那么就不会有符合该条件的行。

英文:

Remember that a WHERE clause is conceptually testing each single row of data against the condition.

The condition awayteam_id = 8634 AND hometeam_id = 8634 will be true only for rows where both the awayteam_id and the hometeam_id are 8634.

To visualise with examples:

awayteam_id hometeam_id awayteam_id = 8634 AND hometeam_id = 8634
8001 8002 false
8001 8634 false
8634 8002 false
8634 8634 true

By the names of the columns, I'm guessing that the table represents sports matches between two teams. If that's the case, the last row would represent a team playing against themselves.

If you don't have any matches like that, then you won't get any rows matching that condition.

huangapple
  • 本文由 发表于 2023年4月19日 21:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定