1. 程式人生 > >MySql分組後統計數量

MySql分組後統計數量

SELECT count(*) FROM 表名 WHERE 條件  這樣查出來的是總記錄條

SELECT count(*) FROM 表名 WHERE 條件 GROUP BY id //這樣統計的會是每組的記錄條數.

select count(*) from (select ...from .....where ......group by .....) a;

select count(*) from(

select
    IFNULL(AVG(a.price),0) currentPrice,
    a.data_time dealDate,
    gc.name className
    from ezs_price_trend a,ezs_goods_class gc,ezs_dict e1,ezs_dict e2 where e1.code like 'EZS_COLOR%' and e2.code like 'EZS_FORM%'
    and  a.goodClass_id = gc.id and a.color_id = e1.id and a.form_id = e2.id and a.type = '3'
    /*多條件查詢*/
     GROUP BY DATE_FORMAT(a.data_time,'%Y-%m-%d') ORDER BY a.data_time desc

) a ;

select
    a.data_time,DATE_FORMAT(a.data_time,'%Y-%m-%d'),
    date_add(DATE_FORMAT(a.data_time,'%Y-%m-%d'),interval 1 MONTH)/*增加月份*/,
    DATE_SUB(DATE_FORMAT(a.data_time,'%Y-%m-%d'),interval 1 MONTH)/*減少月份*/
    from ezs_price_trend a
    /*多條件查詢*/
     GROUP BY DATE_FORMAT(a.data_time,'%Y-%m-%d') ORDER BY a.data_time desc;