php – 带有条件的Mysql案例

前端之家收集整理的这篇文章主要介绍了php – 带有条件的Mysql案例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在 mysql查询中编写一个案例,该案例检查特定列的null或0

CREATE TABLE tblConfirmationStatus (Confirm_Status TINY INT)

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

要求的输出

ConfirmStatus

   Confirmed
   Not Confirmed
   Confirmed
   Not Confirmed
   Not Confirmed
   Confirmed
   Not Confirmed

0或Null – 未确认,1 – 确认

SELECT CASE Confirm_Status 
            WHEN NULL OR 0 THEN 'Not Confirmed' 
             ELSE  'Confirmed' END AS ConfirmStatus
  FROM tblConfirmationStatus;
CASE语句有两种选择 – 您发布的语句或:
SELECT CASE 
        WHEN Confirm_Status IS NULL OR Confirm_Status = 0 THEN 'Not Confirmed' 
        ELSE  'Confirmed' 
       END AS ConfirmStatus

但你可以使用:

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

NULL是缺少值,因此检查大于零的值应该归入与零相同的类别.

原文链接:https://www.f2er.com/php/136441.html

猜你在找的PHP相关文章