简体   繁体   中英

Multiple COUNT SELECTS from the same table in one

how to do multiple COUNT(*) SELECTS from the same table in one ...

$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 ";
    $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

尝试这个

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

You may try this:

$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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM