多个COUNT SELECTS来自同一个表中的一个

时间:2016-12-02 10:10:37

标签: mysql

如何在同一个表中执行多个COUNT(*) SELECTS ...

$query = "SELECT count(*) FROM content_form WHERE read_flag = 0 ";  
$query = "SELECT count(*) FROM content_form WHERE read_flag = 1 "; 
$query = "SELECT count(*) FROM content_form WHERE read_flag = 0 ";

3 个答案:

答案 0 :(得分:2)

    $query = "SELECT sum(Case when read_flag=0 then 1 else 0 end) as ReadFlag0Cnt, 
                     sum(Case when read_flag=1 then 1 else 0 end) as ReadFlag1Cnt
              FROM content_form";
You can use case statement

答案 1 :(得分:2)

试试这个

SELECT read_flag,count(read_flag) as cnt FROM content_form WHERE read_flag IN (0,1,....) GROUP BY read_flag

答案 2 :(得分:1)

你可以试试这个:

$query = "select (SELECT count(*) FROM content_form WHERE read_flag = 0) 
as flag0, (SELECT count(*) FROM content_form WHERE read_flag = 1) as flag1,
(SELECT count(*) FROM content_form WHERE read_flag = 0) as flag0) from content_form";