这是我的查询,并没有在特定行中返回空列或空列值
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
答案 0 :(得分:2)
我并不完全明白你要做什么。如果每列都是空字符串或空字符串,您想要指示它吗?如果是这样,您有一些错误:
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';
另外,对于字符串使用单引号'
而不是双"
,虽然你甚至不需要字符串,但是你要汇总数字,所以请使用数字! / p>