MySQL 处的 Case 语句中布尔值 'Trues' 的求和

Summing of Boolean 'Trues' in a Case Statement at MySQL

我想请问一下,我是否可以在 Case 语句中计算 'Trues'。

t_1 As (
SELECT *,
-- Spot Guide not to be Empty 
CASE 
    WHEN (total_spots_number <> 0 AND (spot1_availability IS NULL AND spot2_availability IS NULL AND spot3_availability IS NULL)) THEN 'cannot be empty'
    WHEN (total_spots_number = 1 AND SUM(spot1_availability IS NOT NULL AND spot2_availability IS NOT NULL AND spot3_availability IS NOT NULL) <> 1) THEN 'one spot is incomplete'
    WHEN (total_spots_number = 2 AND SUM(spot1_availability IS NOT NULL AND spot2_availability IS NOT NULL AND spot3_availability IS NOT NULL) <> 2) THEN 'two spots are incomplete'
    WHEN (total_spots_number = 3 AND SUM(spot1_availability IS NOT NULL AND spot2_availability IS NOT NULL AND spot3_availability IS NOT NULL) <> 3) THEN 'three spots are incomplete'
    ELSE 'nothing to check'
END AS QA_Check_1
FROM Main_Data
)
SELECT * FROM t_1

条件表达式的值为 1 如果它为真,因此您可以简单地添加它们。

WITH availability AS (
    SELECT (spot1_availability IS NOT NULL) + (spot2_availability IS NOT NULL) + (spot3_availability IS NOT NULL) AS availability_count
    FROM Main_Data
)