php实现 sql 查找月,日 分组查询

1,按日查询

public static function getOrderDaysListByUserId($id, $limit, $offset)
    {

        $sql = "SELECT FROM_UNIXTIME(`created`, '%Y-%m-%d') as `days`, ";
        $sql .= "count(*) as day_num ";
        $sql .= "FROM `serve_order` `order` ";
        $sql .= "WHERE `id`=" . $id . " ";
        $sql .= "AND `status`=" . $search['status'] . " ";
        $sql .= "GROUP BY `days` ";
        $sql .= "ORDER BY `days` DESC ";
//        $sql .= "LIMIT {$offset}, {$limit}";

        $data = System_Model_ServeOrder::dataAccess()
            ->nativeSql($sql, array());

        if (empty($data)) {
            return array();
        }

        return $data;
    }

2.按月查询
public static function getOrderMonthsListByUserId($search, $limit, $offset)
    {

        $sql = "SELECT FROM_UNIXTIME(`created`, '%Y-%m') as `months`, ";
        $sql .= "count(*) as month_num ";
        $sql .= "FROM `serve_order` `order` ";
        $sql .= "WHERE `id`=" . $search['id'] . " ";
        $sql .= "AND `status`=" . $search['status'] . " ";
        $sql .= "GROUP BY `months` ";
        $sql .= "ORDER BY `months` DESC ";
//        $sql .= "LIMIT {$offset}, {$limit}";

        $data = System_Model_ServeOrder::dataAccess()
            ->nativeSql($sql, array());

        if (empty($data)) {
            return array();
        }

        return $data;
    }

 

 

 

 

posted @ 2015-12-01 16:30  SilverWang  阅读(590)  评论(0编辑  收藏  举报