laravel join关联查询代码实例

吾爱主题 阅读:203 2021-11-08 15:23:00 评论:0

laravel join关联查询

1、两表关联

?
1 2 3 4 5 6 7 8 9 10 11 12 $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、三表关联

?
1 2 3 4 5 6 7 8 9 10 11 $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 关联查询内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_42390015/article/details/89531474

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容