使用IF-THE或CASE-然后从数据集中进行选择

发布于 2025-01-19 19:11:53 字数 472 浏览 0 评论 0原文

我有一个一年级、二年级和三年级学生的数据集。我想选择一年级和二年级所有学生的姓名,但只选择三年级名为 John、Jane 或 Smith 的学生。到目前为止,这是我所得到的:

select 
first_name, 
grade_level

from table_with_all_students_info

-- I need an if or case when statement here saying if student is in third grade, their first name selected can only be John, Jane or Smith. My attempt is below. 

case when grade_level in ('3rd grade')
then first_name in ('John', 'Jane', 'Smith')

我不确定我在那里出了什么问题,但我希望得到一些帮助。谢谢

I have a dataset with students in 1st, 2nd and 3rd grades. I want to select the names of all students in 1st and 2nd grades, but only students named John, Jane or Smith from 3rd grade. Here is what I have so far:

select 
first_name, 
grade_level

from table_with_all_students_info

-- I need an if or case when statement here saying if student is in third grade, their first name selected can only be John, Jane or Smith. My attempt is below. 

case when grade_level in ('3rd grade')
then first_name in ('John', 'Jane', 'Smith')

I'm not sure what I'm getting wrong there but I'd appreciate some help. Thanks

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

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

发布评论

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

评论(2

辞别 2025-01-26 19:11:53

您可以对两个查询进行 UNION,如下所示:

    select first_name, grade_level
    from table
    where grade_level in ('1st','2nd')
    union
    select first_name, grade_level
    from table
    where grade_level = '3rd'
    and first_name in ('John','Jane','Smith')

You can UNION two queries such as this:

    select first_name, grade_level
    from table
    where grade_level in ('1st','2nd')
    union
    select first_name, grade_level
    from table
    where grade_level = '3rd'
    and first_name in ('John','Jane','Smith')
不念旧人 2025-01-26 19:11:53

whereor 结合使用:

select 
  first_name, 
  grade_level
from table_with_all_students_info
where (grade_level = '3rd grade' and first_name in ('John', 'Jane', 'Smith'))
  or grade_level in ('1st grade', '2nd grade')

如果您的数据中只有 3 个成绩,则可以将 grade_level 替换为 ('1stgrade', '2ndgrade') 与 grade_level != '三年级'

Use where with or:

select 
  first_name, 
  grade_level
from table_with_all_students_info
where (grade_level = '3rd grade' and first_name in ('John', 'Jane', 'Smith'))
  or grade_level in ('1st grade', '2nd grade')

If there are only 3 grades in your data, you can replace grade_level in ('1st grade', '2nd grade') with grade_level != '3rd grade'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文