laravel下join关联查询的操作有什么,怎样实现?
导读:这篇文章给大家分享的是laravel下join关联查询的相关内容,下文介绍了两表关联查询和三表关联查询,文中示例代码介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。laravel join关联查询 1、两表关联 $fbaInvento...
这篇文章给大家分享的是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关联查询的操作就介绍到这,上述示例具有一定的借鉴价值,感兴趣的朋友可以参考,希望能对大家有帮助,想要了解更多大家可以关注其它的相关文章。
文本转载自脚本之家
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: laravel下join关联查询的操作有什么,怎样实现?
本文地址: https://pptw.com/jishu/653283.html