您现在的位置是:群英 > 开发技术 > PHP语言
laravel下join关联查询的操作有什么,怎样实现?
Admin发表于 2021-11-29 17:58:431723 次浏览

    这篇文章给大家分享的是laravel下join关联查询的相关内容,下文介绍了两表关联查询和三表关联查询,文中示例代码介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。

laravel join关联查询

    1、两表关联

$fbaInventoryTb = (new \App\Model\Amz\Fba\InventoryReport)->getTable();
$productTb = (new \App\Model\Amz\Product)->getTable();
$twInventoryTb = (new \App\Model\TWUsa\TwusaInventory)->getTable();
$qry = \DB::table($fbaInventoryTb);
  $qry->select($fbaInventoryTb.'.*')
   ->where($fbaInventoryTb.'.ec_id',1)
   ->leftjoin($productTb, $productTb.'.sku', '=', $fbaInventoryTb.'.sku')
   ->addSelect($productTb.'.id as goods_id',$productTb.'.sku as mfn', $productTb.'.ec_id as pro_ec_id');
//  ->where($productTb.'.ec_id',1); //不应该在此处排除 product 表的ec_id

//  return $qry->toSql();
  $res = $qry->get()->whereIn('pro_ec_id',[1, null] );//leftJion 最后排除不符合条件的

    2、三表关联

$qry = \DB::table($twInventoryTb);
  $qry->select($twInventoryTb.'.*')
   ->leftjoin($fbaInventoryTb, $fbaInventoryTb.'.fnsku', '=', $twInventoryTb.'.product_sn')
   ->addSelect($fbaInventoryTb.'.ec_id')
   ->where($fbaInventoryTb.'.ec_id',1);

  $qry->LeftJoin($productTb, $productTb.'.sku', '=', $fbaInventoryTb.'.sku')
//   ->where($productTb.'.ec_id',1)
   ->addSelect($productTb.'.id as goods_id', $productTb.'.sku as mfn', $productTb.'.ec_id as pro_ec_id');

  $res = $qry->get()->whereIn('pro_ec_id', [1, null]);

    关于laravel下join关联查询的操作就介绍到这,上述示例具有一定的借鉴价值,感兴趣的朋友可以参考,希望能对大家有帮助,想要了解更多大家可以关注其它的相关文章。

文本转载自脚本之家

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: join关联查询
相关信息推荐
2022-09-29 17:50:03 
摘要:这篇文章主要为大家介绍了go-zero微服务实战性能优化极致秒杀功能实现,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
2022-01-14 18:36:22 
摘要:PHP往数据库增加数据的方法是什么?PHP项目中,常常需要往数据库增加数据,那么就需要先连接数据库,然后再获取数据和插入数据,实际该怎样操作呢?下面我们通过具体的示例来了解看看,感兴趣的朋友就继续往下看吧。
2022-07-04 17:29:46 
摘要:这篇文章主要为大家介绍了Go语言基础枚举的用法及示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部