php - Laravel 查询

标签 php mysql laravel

我有 2 个表 itemsbranch_item
我需要优化和查询这个,

$test2 = DB::table('items')
            ->join('branch_item', 'items.id', '=', 'branch_item.item_id')
            ->select('items.minimum', 'branch_item.item_quantity')
            ->where('branch_item.branch_id',9)
            ->where('branch_item.item_quantity','<' ,'items.minimum')
            ->get();
return $test2;

我需要的是查询某分店低于最低数量的商品。

我可以使用 foreach 来做到这一点,但它加载速度太慢,所以我认为我需要使用连接表。

最佳答案

$test2 = DB::table('branch_item')
            ->join('items', 'branch_item.item_id', '=', 'items.id')
            ->select('branch_item.id','items.id','items.minimum', 'branch_item.item_quantity')
            ->where('branch_item.branch_id',9)
            ->having('items.minimum', '>' ,'branch_item.item_quantity')
            ->get();

我们的团队已经调试过了,问题是我们使用 Where 而不是 Having, 有和在哪里有很大的区别。

关于php - Laravel 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27479677/

相关文章:

php - 一个实体的多个 OneToMany 关系

mysql - 将 WSO2API 管理器从 H2 迁移到 Mysql

php - 我对 foreach 理解 laravel 5.6 有疑问

php - Eloquent many-to-many-to-many - 如何轻松加载远距离关系

php - 双下拉列表值

php - 将 Eloquent 模型与业务逻辑分离

php 事件记录::2000、1066,不唯一的表/别名: 'category'

php - 如何使用自动加载 Codeigniter 显示查询计数(*)中的数据?

mysql - 如何为每个客户 ID 提供单独的订单 ID?

php - Laravel Eloquent 模型继承