laravel join关联查询代码实例
更新时间:2021年03月18日 17:25:03 作者:Martin Wu
这篇文章主要介绍了laravel join关联查询代码实例,join联查是写sql查询的时候常用也是高效的一种查询,文中是结合laravel框架来使用联查,有感兴趣的同学可以学习下
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 关联查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解决办法
这篇文章主要介绍了PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解决办法,需要的朋友可以参考下2014-05-05
php中对内置函数json_encode和json_decode的异常处理
这篇文章主要介绍了php中对内置函数json_encode和json_decode的异常处理,本篇文章通过定义描述和列举表格以及最后代码的展示,以下就是详细内容,需要的朋友可以参考下2021-07-07
基于thinkphp6.0的success、error实现方法
这篇文章主要介绍了基于thinkphp6.0的success、error实现方法,本文分步骤给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2019-11-11
laravel Validator ajax返回错误信息的方法
今天小编就为大家分享一篇laravel Validator ajax返回错误信息的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2019-09-09


最新评论