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];
 }
复制代码

查询关联语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   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的查询语句!!! 

posted @   浪、子  阅读(1708)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
历史上的今天:
2018-12-17 php获取用户的上5级用户
点击右上角即可分享
微信分享提示