php 带有 Or 条件的 Mysql 案例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11715607/
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-08-25 01:56:08  来源:igfitidea点击:

Mysql Case with Or condition

phpmysqlsql

提问by user1187

How to write a case in mysql query which checks for null or 0 for a particular column

如何在 mysql 查询中编写一个案例来检查特定列的 null 或 0

CREATE TABLE tblConfirmationStatus (Confirm_Status TINY INT)

INSERT INTO tblConfirmationStatus 
Confirm_Status
VALUES
(1),
(0),
(1),
({null}),
(0),
(1),
({null})

Required Output

所需输出

ConfirmStatus

确认状态

   Confirmed
   Not Confirmed
   Confirmed
   Not Confirmed
   Not Confirmed
   Confirmed
   Not Confirmed

0 or Null - Not Confirmed, 1-Confirmed

0 或 Null - 未确认,1 - 已确认

SELECT CASE Confirm_Status 
            WHEN NULL OR 0 THEN 'Not Confirmed' 
             ELSE  'Confirmed' END AS ConfirmStatus
  FROM tblConfirmationStatus;

回答by OMG Ponies

There's two options for CASE statements - the one you posted, or:

CASE 语句有两种选择 - 您发布的一种,或者:

SELECT CASE 
        WHEN Confirm_Status IS NULL OR Confirm_Status = 0 THEN 'Not Confirmed' 
        ELSE  'Confirmed' 
       END AS ConfirmStatus

But you could probably use:

但你可能会使用:

SELECT CASE 
        WHEN Confirm_Status > 0 THEN 'Confirmed' 
        ELSE  'Not Confirmed' 
       END AS ConfirmStatus

NULLis the absence of a value, so checking for values above zero should fall into the same category as zero.

NULL是缺少值,因此检查大于零的值应该与零属于同一类别。

回答by Omesh

SELECT IF((Confirm_Status IS NULL OR Confirm_Status = 0), 
           'Not Confirmed', 'Confirmed') AS ConfirmStatus
FROM tblConfirmationStatus;

回答by Adriaan Stander

Have you had a look at using IFNULLoperator.

您是否看过使用IFNULL运算符。

IFNULL(expr1,expr2)

If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used.

IFNULL(expr1,expr2)

如果 expr1 不为 NULL,则 IFNULL() 返回 expr1;否则返回 expr2。IFNULL() 返回数字或字符串值,具体取决于使用它的上下文。