[sql情况什么时候]为什么我同时指定两个条件时何时无法工作?

发布于 2025-02-03 10:04:41 字数 1972 浏览 0 评论 0原文

嗨,我正在尝试从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)

teams_spain表就像这样

matches_spain表就像这样

“ 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
teams_spain table

and

matches_spain table is like this
matches_spain table

"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?

desirable output is
desirable output
my wrong output is
something wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷心人i 2025-02-10 10:04:41

除了我不明白您为什么要像自己一样更改逻辑的事实外,当构建构造的主队和客队团队时,您都会使用对他们的认同结果。第二个情况不正确。您写道:

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

...反之亦然:

case when hometeam_id=8634 and awayteam_id = 8633 then 'Real Madrid CF'
 when hometeam_id=8633 and awayteam_id = 8634 then 'FC Barcelona'
 end as away

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 second CASE WHEN is incorrect. You wrote:

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

...this should be vice versa:

case when hometeam_id=8634 and awayteam_id = 8633 then 'Real Madrid CF'
 when hometeam_id=8633 and awayteam_id = 8634 then 'FC Barcelona'
 end as away
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文