duandaotui5633 2017-01-13 09:13
浏览 56
已采纳

查询与多个join /存在与不同where子句的相同关系

here is my table

enter image description here i want product_id that have both these conditions

 `f_id` = '4' and `value` in ('10', '11', '12', '13' )

and

 `f_id` = '10' and `value` in ('34')

which would be product_id : 11 here is my query

$products = Product::whereHas('ProductFilter' , function($q) use ($filter_groups){

        foreach($filter_groups as $k=>$v)
        $q->where('f_id' , $k )->whereIn( 'value' ,$v );

    })->get();

which will result in this query

select * from `products` where exists 

( 
  select * from `product_filters` where 
 `product_filters`.`product_id` = `products`.`id` and 
 `f_id` = '4' and `value` in ('10', '11', '12', '13' )   and 
 `f_id` = '10' and `value` in ('34')

)

but there is no relations between f_id and value and it wont return any result

basically what i want is this query

select * from `products` where exists 

( 
  select * from `product_filters` where 
 `product_filters`.`product_id` = `products`.`id` and 
 `f_id` = '4' and `value` in ('10', '11', '12', '13' )  

)
and exists 
( 
  select * from `product_filters` where 
 `product_filters`.`product_id` = `products`.`id` and 
 `f_id` = '10' and `value` in ('34')

)
  • 写回答

2条回答 默认 最新

  • dongxiji0687 2017-01-13 17:35
    关注
    $query = Product::where([]);
    foreach($filter_groups as $k=>$v)
    $query->whereHas('ProductFilter' , function($q) use ($k , $v ){
            $q->where('f_id' , $k )->whereIn( 'value' ,$v );
        })
    
    $products = $query->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题