SQL/数据库方面(持续更新系列)

将数据库中查出的列表以指定的 id 作为数组的键名

/**
 * @param $arr
 * @param $key_name
 * @return array
 * 将数据库中查出的列表以指定的 id 作为数组的键名 
 */
function convert_arr_key($arr, $key_name)
{
    $result = array();
    foreach($arr as $key => $val){
        $result[$val[$key_name]] = $val;
    }
    return $result;
}

查询当前时间方面的时间处理

    /**
     * 统计当前门店当前用户在某个时间段(今天、本周、本月、本年)的订单数量
     * @param $time 时间段(cur_day cur_week cur_month cur_year)
     * @param $owner_m_id
     * @param $creator_role_id
     * @return int
     */
    public function statisticalOrder($time,$owner_m_id,$creator_role_id) {
        //根据参数选择开始-结束时间 条件
        if($time == 'cur_day') {
            //今日开始-结束时间戳
            $start_time = strtotime(date('Y-m-d 00:00:00',time()));
            $end_time   = ($start_time+86400);
        }elseif($time == 'cur_week') {
            //本周开始-结束时间戳
            $start_time = mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"));
            $end_time   = mktime(23,59,60,date("m"),date("d")-date("w")+7,date("Y"));
        }elseif($time == 'cur_month') {
            //本月开始-结束时间戳
            $start_time = strtotime(date('Y-m-1').'-1 day')+24*60*60;
            $end_time   = strtotime(date('Y-m-1 00:00:00',strtotime('next month')));
        }elseif($time == 'cur_year') {
            //本年开始时间-结束时间戳
            $start_time = strtotime(date('Y-1-1 00:00:00',time()));
            $end_time   = strtotime(date('Y-1-1 00:00:00',strtotime('+1 year')));
        }
        $where = array();
        $where['c.order_time'] = array(['>',$start_time],['<',$end_time],'AND');
        return Db::name('customer')->alias('a')
            ->where($where)
            ->join('customerInvite b','a.customer_id=b.customer_id','left')
            ->join('order c','b.invite_id=c.invite_id','left')
            ->count();
    }

获取指定分类的所有子分类ID(不需要自身id就子集unset掉)

    /**
     * 获取指定分类的所有子分类ID号(包括自己的id)
     * @param $categoryID
     * @return array
     */
    public static function getAllChildcateIds($categoryID)
    {
        //初始化ID数组
        $array[] = $categoryID;
        do
        {
            $ids = '';
            $where['parent_id'] = array('in',$categoryID);
            $cate = Db::name('merchant')->where($where)->select();
            foreach ($cate as $k=>$v)
            {
                $array[] = $v['mid'];
                $ids .= ',' . $v['mid'];
            }
            $ids = substr($ids, 1, strlen($ids));
            $categoryID = $ids;
        }
        while (!empty($cate));
        //返回数组
        return $array;
    }

猜你喜欢

转载自blog.csdn.net/qq_33273884/article/details/81941562