SQL 'select' 查询有 3 个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13552180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:18:54  来源:igfitidea点击:

SQL 'select' Query with 3 conditions

sql

提问by sanu j

SELECT * 
FROM REVIEW 
WHERE REVIEWERID =5 AND APPRAISEECONFIRMYN='Y' AND HRCONFIRMYN = NULL

Are 2 'AND' conditions allowed like this? I'm not getting the correct output. There are 2 records in the database that fulfil the above conditions. When I remove the last condition 'HRCONFIRMYN = NULL'and execute, I get the correct output.

像这样允许 2 个“AND”条件吗?我没有得到正确的输出。数据库中有 2 条记录满足上述条件。当我删除最后一个条件'HRCONFIRMYN = NULL'并执行时,我得到了正确的输出。

How can this be solved? I need to check all 3 conditions whilst searchng the records.

如何解决这个问题?我需要在搜索记录时检查所有 3 个条件。

回答by Mahmoud Gamal

To compare the NULLvalues, you have to use the IS NULLpredicate instead of = NULLlike so:

要比较这些NULL值,您必须使用IS NULL谓词而不是= NULL像这样:

SELECT * 
FROM REVIEW 
WHERE REVIEWERID = 5 
  AND APPRAISEECONFIRMYN = 'Y' 
  AND HRCONFIRMYN IS NULL

回答by John Woo

  • use ORinstead
  • group your condition
  • use IS NULLwhen comparing with NULLs
  • 使用OR替代
  • 分组你的条件
  • 使用IS NULL与空值进行比较时,

query,

询问,

SELECT * 
FROM REVIEW 
WHERE (REVIEWERID =5 AND APPRAISEECONFIRMYN='Y') OR
      HRCONFIRMYN IS NULL