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

3 篇文章 0 订阅

desc和asc(容易混)

desc是descend 降序意思 
asc 是ascend 升序意思

将数据库中查出的列表以指定的 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;
	}




/**
     * 2018-7-21
     * 获取当前部门的所有子部门的id(包括自己的id)
     * @param $id
     * @return array
     */
    public function getChildsIds($id) {
        $data =  Db::query("select getDepartmentChildList('{$id}')");
        $return_data = explode(',',$data[0]["getDepartmentChildList('{$id}')"]);
        array_shift($return_data);
        return $return_data;
    }

在tp5中使用paginate方法后,在其对象里面添加一个新的字段信息

     $map =array();
     $action_data =  ActivityGood::where($map)->where('act_id',$action_id)->where('m_id',$m_id)->paginate();//这里的查询必须是类的查询,需要的是对象形式
     foreach ($action_data as $key => &$value) {
            $value['type'] = '<a class="img-link" href="'. $value['pro_pic'].'"
                    data-toggle="tooltip"
                    title="点击查看大图"
                    target="_blank">
                    <img class="image" src="'. $value['pro_pic'].'"></a>';

        }

事务处理

     Db::startTrans();
            try{
               //处理函数
                //提交事务
                Db::commit();
            } catch (\Exception $e) {
                //回滚事务
                Db::rollback();
                $this->error('修改联系人失败'.$e->getMessage());
            }
            $this->success('修改联系人成功',url('customer/index/edit'));

树状图使用

    /**
     * 2018-8-30
     * 将数据进行树状整理,并添加树枝效果
     * @param $list
     * @param int $pid
     * @param string $itemprefix
     * @return array
     */
    function transformerTree($list,$pid=0,$itemprefix = '') {
        static $icon = array('│', '├', '└');
        static $nbsp = "&nbsp;";
        static $arr = array();
        $number = 1;
        foreach($list as $row) {
            if($row['pid'] == $pid) {
                $brotherCount = 0;
                //判断当前有多少个兄弟分类
                foreach($list as $r) {
                    if($row['pid'] == $r['pid']) {
                        $brotherCount++;
                    }
                }
                if($brotherCount >0) {
                    $j = $k = '';
                    if($number == $brotherCount) {
                        $j .= $icon[2];
                        $k = $itemprefix ? $nbsp : '';
                    }else{
                        $j .= $icon[1];
                        $k = $itemprefix ? $icon[0] : '';
                    }
                    $spacer = $itemprefix ? $itemprefix . $j : '';
                    $row['name'] = $spacer.$row['name'];
                    $arr[] = $row;
                    $number++;
                    transformerTree($list,$row['id'],$itemprefix . $k . $nbsp);
                }
            }
        }
        return  $arr;

简易获取信息的做法,主要用在编辑页面

$row = $this->model->get(['id' => $ids]);   //这里就能获取到该条数据的对象了,这样子我们需要用到里面的值的时候,					  
$name = $row->name                          //我们只需要用对象的形式去调用它就可以了
										    

时间函数处理

    /**
     * 统计当前门店当前用户下的下属用户在某个时间段(今天、本周、本月、本年)的新增客户数量
     * @param $time (cur_day cur_week cur_month cur_year)
     * @param int $owner_m_id    当前门店id
     * @param int $owner_role_id 当前客户所有者 用户id
     * @return int 客户数量
     */
    public function statisticalCustommanager($time,$owner_m_id,$v) {
        //根据参数选择开始-结束时间 条件
        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['create_time']    = array(['>',$start_time],['<',$end_time],'AND');
        $where['owner_m_id']    = $owner_m_id;
        $where['owner_role_id'] = $v;
        return Db::name('customer')->where($where)->count();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值