如果我想获得数据库中某个表的总数和符合某条件的记录数,可以只查询一次实现吗?
例如有个表people,里面有name字段,我想获得people里的所有记录数和name里带'Tom'的记录数。只查询一次!
谢谢

解决方案 »

  1.   

    select count(*),sum(if(name='Tom',1,0))
    from people
    上面虽然是一个SQL语句,但效率并不高。select (select count(*) from people),(select count(*) from people where name='Tom');
      

  2.   

    流程控制函数:
    http://dev.mysql.com/doc/refman/5.1/zh/functions.html#control-flow-functions
      

  3.   

    select count(*) as Total_Count,sum(case when name='Tom' then 1 else 0 end) as Tom_Count from people;