我需要帮助验证mysql中的空值或空值 我试图在我的情况下使用这种方式,但不起作用:
SELECT
CASE
WHEN account_type IS NULL THEN 'Uninformed'
WHEN account_type = '' THEN 'Empty '
END as Type,
COUNT(accounts.id) AS Total
FROM
accounts
有谁知道如何解决这个问题?
答案 0 :(得分:1)
SELECT
CASE WHEN account_type IS NULL THEN 'Uninformed'
WHEN account_type = '' THEN 'Empty '
END as Type,
COUNT(accounts.id) AS Total
FROM accounts
group by account_type
您缺少group by
条款。
答案 1 :(得分:1)
您的查询是正确的。最后,您需要添加group by account_type;
。
查询应该是这样的。
SELECT
CASE WHEN account_type IS NULL THEN 'Uninformed'
WHEN account_type = '' THEN 'Empty '
END AS Type,
COUNT(accounts.id) AS Total
FROM accounts
GROUP BY account_type
答案 2 :(得分:1)
在没有case语句的情况下编写相同查询的另一种流畅方式就是......
SELECT ISNULL(NULLIF(ISNULL(account_type , 'Uninformed'),''),'Empty') AS Type
,COUNT(accounts.id) AS Total
FROM accounts
GROUP BY ISNULL(NULLIF(ISNULL(account_type , 'Uninformed'),''),'Empty')