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