laravel join关联查询代码实例

admin3年前PHP教程33
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 关联查询代码实例的文章就介绍到这了,更多相关laravel join 关联查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

免责声明:本文内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请核实广告和内容真实性,谨慎使用。

相关文章

PHP7下安装并使用xhprof性能分析工具

该 xhprof 版本是从 github/longxinH/xhprof 获取安装 xhprofcd xhprof/extension/phpize./configure make然后在/e...

php中cookie与session的区别点总结

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑无论是在系统运维还是 PHP 开发人员的面试中,经常会被问到 Session 和 Cookie 在 PHP 中的区别?下面我们就...

PHP并发场景的三种解决方案代码实例

在秒杀,抢购等并发场景下,可能会出现超卖的现象,在 PHP 语言中并没有原生提供并发的解决方案,因此就需要借助其他方式来实现并发控制,其实方案有很多种,今天只是举个栗子抛砖引玉,有其他更好的方案你可以...

如何选择韩国多ip服务器才正确?韩国多ip服务器租用地址是多少?

由于韩国地域的特殊性,使用服务器可以免备案,许多企业开始选择韩国多ip服务器,接下来我们来谈谈如何选择韩国多ip服务器才正确?1.韩国多ip服务器的速度韩国多ip服务器的运转速度直接影响着站群的呼应时...

php实现自运行的实例详解

说明1、创建一个PHP示例文件;然后输入“ignore_user_abort();”。2、通过“do{$fp = fopen('test.php',...

租用高防服务器哪些因素重要?国内高防大带宽服务器租用注意事项?

租用一台独立的高防服务器,用户可以享受到最高的管理权限。高防服务器供应商,应保证高防服务器的硬件可支持服务水平协议提供稳定的网络和电源连接。用户可以自行操作高防服务器中的环境搭建、软件安装、系统重装等...