巧用tp5.1,实现SQL语句。实现or查询

SQL 语句:
SELECT * FROM `tp_product` WHERE  `money` >= 1  AND `money` <= 1111  AND (  ( FIND_IN_SET(0,label_ids) ) OR `title` LIKE '%名称%' ) ORDER BY `money` DESC
/**
     * @title 前台商品搜索框信息
     * @description 前台商品搜索框信息
     * @author dhl
     * @url /product/front_product/search_product
     * @param name:token require:1 default: desc:token
     * @param name:content require:1 default: desc:搜索的内容
     * @param name:category_id require:1 default: desc:需要搜索的分类id
     * @param name:order require:1 default: desc:智能排序值:id
     * @param name:evaluate_score require:1 default: desc:好评优先值:desc
     * @param name:sales require:1 default: desc:人气优先值:desc
     * @param name:moneystart require:1 default: desc:开始价格区间
     * @param name:moneyend require:1 default: desc:介绍价格区间
     * @param name:pricedesc require:1 default: desc:价格由高到低值:desc
     * @param name:priceasc require:1 default: desc:价格由低到高值:asc
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @return error_id:1-06-01-0005 校验失败 1-06-01-0007 获取失败
     */
    public function search_product(){
        $data=input();
        $validate = new Validate([
            'content'  => 'require',
        ]);
        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }
        foreach ($data as $k=>$v){
            if($v === 0){
                continue;
            }
            if ($v === ''){
                unset($data[$k]);
            }
        }
        $where = array();
//         dump($data);exit();
        if(!empty($data['moneystart'])&&!empty($data['moneyend'])){
            $start = ($data['moneystart']);
            $end = ($data['moneyend']);
            $where[]=['money','>=',$start];
            $where[]= ['money','<=',$end];
        }elseif(!empty($data['moneystart'])&&empty($data['moneyend'])){
            $start = ($data['moneystart']);
            $where[]=['money','>=',$start];
        }elseif(empty($data['moneystart'])&&!empty($data['moneyend'])){
            $end = ($data['moneyend']);
            $where[]= ['money','<=',$end];
        }
        //筛选只能选择一个,但是和价格不冲突
        foreach ($data as $k=>$v){
            if($k==='order'){
                $x = 'id';
                $y = $v;
            }
            if($k==='evaluate_score' || $k==='sales'){
                $x = $k;
                $y = $v;
            }
            if($k==='pricedesc' || $k==='priceasc'){//价格倒序,正序
                $x = 'money';
                $y = $v;
            }
        }
        //如果有category_id,查所有的分类子id
        if (!empty($data['category_id'])){
            $ids = Db::name('category')
                ->where('parent_id',$data['category_id'])
                ->where('status',1)
                ->column('id');
            $ids[]=$data['category_id'];
            $where[]=['category_id','in',$ids];
        }
        //查产品的顶级分类
        $msges['category'] = Db::name('category')
            ->where('parent_id',0)
            ->where('status',1)
            ->where('social_category',0)
            ->order('id asc')->field('id,name')->limit(3)->select();
        //查攻略
        $strategy=get_curl('strategy','/strategy/search_strategy',['token'=>$data['token'],'content'=>$data['content']]);
        $msges['strategy'] = $strategy;

        //查产品
        if (empty($x)){
            $x='id';
            $y='asc';
        }
        $msges['product']=Db::name('product')
            ->where($where)
//                ->where('FIND_IN_SET(:id,label_ids)',['id' => $id],'OR','title','like','%'.$data['content'].'%')
            ->where(function ($query) {
                $data=input();
                //查标签
                $label = get_curl('label', 'label/info', ['title' => $data['content']]);
                if ($label['status'] == false || $label['data'] == null){
                    $id = 0;
                }else{
                    $id=$label['data']['id'];
                }
                $query->where('FIND_IN_SET('.$id.',label_ids)')
                    ->whereOr('title','like','%'.$data['content'].'%');
            })
            ->withAttr('label_ids',function ($value,$data){
                $msg=input();
                $label = explode(',',$data['label_ids']);
                $label_names= array();
                foreach ($label as $k =>$v){
                    $label_name = get_curl('label', 'label/info', ['id' => $v]);
                    if ($label_name['status'] == true) {
                        /*unset($data['label_ids'][$k]);
                        continue; */
                        $label_names[] = $label_name['data']['title'];
                    }

                }
                return $label_names;
            })
            ->order($x,$y)
            ->paginate();
        $msg['data']=$msges;
        $msg['msg'] = '搜索成功';
        return $this->successJson($msg);

    }

你可能感兴趣的:(数据库)