这篇文章主要介绍了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 关联查询内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
织梦狗教程
本文标题为:laravel join关联查询代码实例


基础教程推荐
猜你喜欢
- PHP数据加密方式梳理介绍 2023-07-03
- laravel model模型定义实现开启自动管理时间created_at,updated_at 2023-03-02
- PHP实现创建一个RPC服务操作示例 2023-04-01
- PHP删除数组中指定值的元素常用方法实例分析【4种方法】 2022-11-12
- PHP实现生成数据字典功能示例 2022-10-18
- PHP使用SMTP邮件服务器发送邮件示例 2022-11-16
- php中使用array_filter()函数过滤数组实例讲解 2023-05-19
- thinkPHP3.2.2框架行为扩展及demo示例 2022-11-07
- TP5 连接多个数据库及使用方法 2023-08-30
- TP5(thinkPHP5框架)基于bootstrap实现的单图上传插件用法示例 2023-01-19