这篇文章主要介绍了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基于反射机制实现自动依赖注入的方法详解 2022-10-02
- php如何获取当前日期和星期 2024-12-06
- php去除deprecated的实例方法 2022-09-02
- Laravel 解决composer相关操作提示php相关异常的问题 2023-03-08
- PHP采用get获取url汉字出现乱码的解决方法 2024-03-28
- PHP laravel使用自定义邮件类实现发送邮件 2023-07-03
- php+mysql开发的最简单在线题库(在线做题系统)完整案例 2023-01-04
- Laravel修改验证提示信息为中文的示例 2023-03-08
- php生成短网址/短链接原理和用法实例分析 2023-04-20
- PHP字符串函数系列之nl2br(),在字符串中的每个新行 (\n) 之前插入 HTML 换行符br 2024-03-28