mysql的统计数据!

        /*计算昨日计费数据*/
        $sql = 'SELECT SUM(money) as yesterdaymoney';
        $sql.= ' FROM yst_consumption ';
        $sql.= " WHERE username in ($userstr)";
        $sql.= ' AND TO_DAYS( NOW( ) ) - TO_DAYS(addtime) = 1';
  
        /*计算本月计费数据*/
        $sql = 'SELECT SUM(money) as monthmoney';
        $sql.= ' FROM yst_consumption ';
        $sql.= " WHERE username in ($userstr)";
        $sql.= " AND DATE_FORMAT(addtime, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )";
   
        /*计算上月计费数据*/
        $sql = 'SELECT SUM(money) as lastmonth';
        $sql.= ' FROM yst_consumption ';
        $sql.= " WHERE username in ($userstr)";
        $sql.= " AND DATE_FORMAT(addtime, '%Y-%m' ) = DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH),'%Y-%m')";
 
        /*计算最近7天的平均消费*/
        $sql = 'SELECT SUM(money) as avg';
        $sql.= ' FROM yst_consumption ';
        $sql.= " WHERE username in ($userstr)";
        $sql.= " AND DATE_FORMAT(addtime, '%Y-%m-%d' ) > DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 7 day),'%Y-%m-%d')";

查询出结果以后再除以7即可!

你可能感兴趣的:(mysql的统计数据!)