如何通过条件求和COUNT函数求和

问题描述 投票:0回答:1

[这里,我的桌子:

id | marital_stat |
====================
1      divorced 
2      divorced 
3      married 
4      single

我想分开每种婚姻状况的总和,因为条件在我的表中使用varchar而不是integer

这是我在模型文件中的查询:

SELECT
COUNT(CASE WHEN marital = 'divorced'   THEN 1 END) AS divorcestat,
COUNT(CASE WHEN marital = 'married' THEN 1 END) AS marriedstat,
COUNT(CASE WHEN marital = 'single' THEN 1 END) AS singlestat
FROM status_tbl

通过上面的查询产生了我想要的值,结果是:

    divorcestat | marriedstat | singlestat
--------------------------------------------
    2               1              1

但是现在我需要将以上所有值加起来,并从Total函数中生成为(divorcestat+marriedstat+singlestat) -> TotalCount。我该怎么做?我已经在stackoverflow中尝试了一些答案,但没有任何效果。 Group by, Union,只是不起作用。

php sql codeigniter
1个回答
0
投票

您可以使用count(*)


0
投票

您可以添加count(*)

© www.soinside.com 2019 - 2024. All rights reserved.