thinkphp6实现关联多个id以逗号分开查询数据


组装查询条件条件  注意这里store_ids是数组格式

 
 if(isset($params['store_ids'])&&!empty($params['store_ids'])) {
            $store_ids=$params['store_ids'];
            $sql_where='';
            foreach($store_ids as $v){
                $sql_where .= "FIND_IN_SET('{$v}',t1.store_ids) or ";
            }
            $sql_where = rtrim($sql_where," or ");
            $map=Db::raw($sql_where);
            $where[]=['','exp',$map];
 }

查询关联语句

    public function getStoreGuidePlanList($where=[],$page,$limit)
    {
        $result=$this->db->name('store_guide_plan')
            ->alias('t1')
            ->field('t1.plan_id,t1.plan_name,t1.status,t1.store_ids,GROUP_CONCAT(t2.store_name) store_name,t1.create_time')
            ->leftJoin('store t2', 'FIND_IN_SET(t2.store_id,t1.store_ids)')
            ->where('t1.delete_time',0)
            ->where($where)
            ->order(['t1.plan_id' => 'desc'])
            ->group('t1.plan_id');

        $result->page($page, $limit);
        $count = $result->count();
        $list = $result->select()->toArray();
        return compact('list','count');

 }

 这样就实现了关联多个id的查询语句!!! 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM