这篇文章主要介绍了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数组函数序列之array_sum() – 计算数组元素值之和 2024-01-15
- PHP手机短信验证码实现流程详解 2022-10-18
- PHP实现文件下载【实例分享】 2024-04-27
- PHP判断一个字符串是否是回文字符串的方法 2024-01-31
- php实现构建排除当前元素的乘积数组方法 2022-11-23
- Yii框架连表查询操作示例 2023-02-13
- php实现数组筛选奇数和偶数示例 2024-02-05
- 设定php简写功能的方法 2023-03-17
- PHP实现抽奖系统的示例代码 2023-06-26
- PHP+MySQL+sphinx+scws实现全文检索功能详解 2023-01-31
