这篇文章主要介绍了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关联查询代码实例
基础教程推荐
猜你喜欢
- CentOS 7 编译安装PHP7 2023-09-02
- PHP如何将头像图片转换圆形图片 2023-08-30
- PHP5.0 TIDY_PARSE_FILE缓冲区溢出漏洞的解决方案 2022-11-26
- php+mysql+ajax 局部刷新点赞/取消点赞功能(每个账号只点赞一次) 2023-04-24
- 解析PHP中Exception异常机制 2023-06-13
- PHP树形结构tree类用法示例 2022-12-15
- php fread函数使用方法总结 2023-01-19
- 在laravel中实现事务回滚的方法 2023-02-22
- PHP设计模式之状态模式定义与用法详解 2022-10-12
- PHP中的输出缓冲控制详解 2023-06-03
