【问题标题】:Using conditions for where clause in postgresql select query postgresql在postgresql中使用where子句的条件选择查询postgresql
【发布时间】:2014-02-27 18:52:00
【问题描述】:

查询

select * from notice join noticeconcerned using(notice_id) where (nconcerned_section = 'IT' and nconcerned_batch = 2010) or (nconcerned_batch = 2010) order by n_removeby

结果

a http://www.4shared.com/download/sgAkE2pKce/notice1.png?lgfp=3000

我想显示(2010 批次和 CS 部分)或(2010 批次但没有部分,即空部分)的学生记录所以在这种情况下,应该只显示一行。

我的尝试

select * from notice join noticeconcerned using(notice_id) where case when (nconcerned_section = 'CS' and nconcerned_batch = 2010) then(____What to use here?) END or case when (nconcerned_batch = 2010 and nconcerned_section=null) then(___what to use here?) END order by n_removeby

如果您能解释如何将case statement 用于where clause,那将非常有帮助。我发现了一些关于它的帖子,但这让我感到困惑。

【问题讨论】:

  • 请放一些数据样本(notice 和 noticeconcerned),以及你期望的结果!
  • 我的错误是当我将数据插入表时,我没有插入null,而是插入了' ',即space。所以is null对我不起作用。我更新了该表并使用了查询。它按预期工作。

标签: sql postgresql case where-clause conditional-statements


【解决方案1】:

在您的原始查询中,将 IT 更改为 CS,添加并且 nconcerned_section 在第二个条件下为空,它应该只返回一个结果:

select * from notice join noticeconcerned using(notice_id) where (nconcerned_section = 'CS' and nconcerned_batch = 2010) or (nconcerned_batch = 2010 and nconcerned_section is null)  order by n_removeby

【讨论】:

  • 我的错误是当我向表中插入数据时,我没有插入null,而是插入了' ',即space。所以is null对我不起作用。我更新了该表并使用了您的查询。它按预期工作得很好。谢谢。
【解决方案2】:

我的错误是当我将数据插入表中时,我没有插入null,而是插入了' ',即space。所以is null 对我不起作用。我更新了表并使用了查询

select * from notice join noticeconcerned using(notice_id) where (nconcerned_section = 'CS' and nconcerned_batch = 2010) or (nconcerned_batch = 2010 and nconcerned_section is null) order by n_removeby

现在可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    相关资源
    最近更新 更多