欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

laravel join关联查询代码实例

程序员文章站 2022-06-22 14:14:04
laravel join关联查询1、两表关联$fbainventorytb = (new \app\model\amz\fba\inventoryreport)->gettable();$pro...

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 关联查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!