search

Home  >  Q&A  >  body text

mysql 一个sql 返回多个总数

文章链接:https://segmentfault.com/a/11...
说道:
一个sql 返回多个总数
$sql = "select count(*) all, " ;
$sql .= " count(case when status = 1 then status end) status_1_num, ";
$sql .= " count(case when status = 2 then status end) status_2_num ";
$sql .= " from table_name";

疑问:这条sql是否有问题,我去尝试了,报错,以前类似查询我都用多天sql的,这种都没写过,疑问多多!

ringa_leeringa_lee2784 days ago1025

reply all(3)I'll reply

  • 阿神

    阿神2017-04-17 16:10:37

    all is indeed the keyword, just change it
    If you want the total number of each statusselect status,count(*) status_num from table where status in (1,2) group by status;

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 16:10:37

    Use AS for alias
    count(*) AS number

    reply
    0
  • PHPz

    PHPz2017-04-17 16:10:37

    There is a problem with select count(*) all
    all is the keyword of mysql, just change it to another name

    reply
    0
  • Cancelreply