这是我的查询,它不会在特定行中返回空或空列值

SELECT if(b2b_image1 is not null or b2b_image1='' ,"1", "0") + if(b2b_image2 is not null or b2b_image2='', "1", "0") + if(b2b_image3 is not null or b2b_image3='', "1", "0") + if(b2b_image4 is not null or b2b_image4='', "1", "0") as non_empty FROM b2b_checkin_report WHERE b2b_booking_id='105';

这里,第 4 列 b2b_image4 是空的,但它返回我 4 作为输出
喜欢...
non_empty
   4

最佳答案

我没有完全理解你想要做什么。如果每列是空字符串或空字符串,您想要指示它吗?如果是这样,你有几个错误:

SELECT if(b2b_image1 is null or b2b_image1='' ,'1', '0')
      ,if(b2b_image2 is null or b2b_image2='', '1', '0')
      ,if(b2b_image3 is null or b2b_image3='', '1', '0')
      ,if(b2b_image4 is null or b2b_image4='', '1', '0')
FROM b2b_checkin_report
WHERE b2b_booking_id='105';

或者,您想查看空或空列的计数,在这种情况下,您的问题是您使用了 IS NOT NULL 而不是 IS NULL ,这与您的意图完全相反:
SELECT if(b2b_image1 is null or b2b_image1='' ,1, 0)
        + if(b2b_image2 is null or b2b_image2='', 1, 0)
        + if(b2b_image3 is null or b2b_image3='', 1, 0)
        + if(b2b_image4 is null or b2b_image4='', 1, 0) as empty_cnt
FROM b2b_checkin_report
WHERE b2b_booking_id='105';

此外,对于字符串,请使用单引号 ' 而不是双引号 " ,尽管您在这里甚至不需要字符串,但您正在对数字求和,因此请使用数字!。

关于mysql - 我的查询没有从表中返回空值或空值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37316872/

10-12 17:25