[sql情况什么时候]为什么我同时指定两个条件时何时无法工作?
嗨,我正在尝试从Datacamp给出的其他版本的SQL查询。
我提供的查询是
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);
我试图调整它
思考:“与其为每种情况方案设置一个条件,让我们通过将两个条件与'和连接起来同时设置两个条件”
我的查询如下。
select date,
case when hometeam_id=8634 and awayteam_id = 8633 then 'FC Barcelona'
when hometeam_id=8633 and awayteam_id = 8634 then 'Real Madrid CF'
end as home,
case when hometeam_id=8633 and awayteam_id = 8634 then 'Real Madrid CF'
when hometeam_id=8634 and awayteam_id = 8633 then 'FC Barcelona'
end as away
from matches_spain
where (hometeam_id=8634 and awayteam_id=8633)
or (hometeam_id=8633 and awayteam_id=8634)
和
“ Matches_spain”是一些赛季中足球比赛的记录,并带有主队ID和Awary Team ID。每个团队都有自己独特的ID,并且可以在Teams_Spain表中找到独特的ID。
Hi I was trying to a different version of SQL query that I was given from datacamp.
The query I was provided with is
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);
I tried to tweak it
thinking, "instead of setting one condition for each case scenario, let's set two conditions at the same time by connecting the two conditions with 'and "
My query is as below.
select date,
case when hometeam_id=8634 and awayteam_id = 8633 then 'FC Barcelona'
when hometeam_id=8633 and awayteam_id = 8634 then 'Real Madrid CF'
end as home,
case when hometeam_id=8633 and awayteam_id = 8634 then 'Real Madrid CF'
when hometeam_id=8634 and awayteam_id = 8633 then 'FC Barcelona'
end as away
from matches_spain
where (hometeam_id=8634 and awayteam_id=8633)
or (hometeam_id=8633 and awayteam_id=8634)
teams_spain table is like this
and
matches_spain table is like this
"matches_spain" is a record of soccer games during some season with home team ids and away team ids. Each team has its own unique id and the unique ids can be found in teams_spain table.
Why does my alternative code print wrong output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了我不明白您为什么要像自己一样更改逻辑的事实外,当构建构造的主队和客队团队时,您都会使用对他们的认同结果。第二个
情况
不正确。您写道:...反之亦然:
Beside the fact I don't understand why you want to change the logic like you did, you are using the identic
CASE WHEN
construct for both home team and away team, so of course, you will get the identic outcome for them. The secondCASE WHEN
is incorrect. You wrote:...this should be vice versa: