Multiple Where In(多在何处)
问题描述
我正在使用 eloqent 过滤一组产品:
I'm using eloqent to filter a set of products:
Product::whereIn('color', $color)->whereIn('size', $size)->whereIn('price', $price)->get();
以上每个变量都是一个id数组
Each of the above variables is an array of ids
$color = [1,2,4,5]
我的问题是,当用户无法通过一组变量发送时,这是否效率低下,比如他们不想要任何滤色器,因此数组将是:
My question is, is this inefficient when the user fails to send through a set of variables, say they did not want any color filters so the array would be:
$color = [];
我已经尝试过 ->toSql 并且它生成了 sql 语句:
I've tried ->toSql and it produces the sql statement:
select * from `products` where `color` in (?, ?) and 0 = 1 and `price` in (?, ?, ?, ?, ?)
上面没有发送尺寸过滤器.
In the above no size filter has been sent through.
0 = 1 是什么意思?这是一种低效的处理方式吗?
What does 0 = 1 mean? And is this an inefficient way of handling things?
推荐答案
这是个很有趣的问题.
0 = 1 将始终为假,因此您的查询将返回零行.但这是为什么呢?
0 = 1 will always be false, so your query will return zero rows. But why is this?
因为通过设置
->whereIn('size', $size)
Laravel 假设您始终希望返回的行具有传递数组中的大小之一.如果你没有在数组中传递任何值,Laravel 不能这样做 where size IN ()
因为它会是语法错误(你基本上说给我所有匹配这个大小的行,但你做不通过大小).因此,如果数组为空,则只需放置 0 = 1
.
Laravel assumes that you always want the returned rows to be with one of the sizes in the passed array. If you do not pass any values in the array, Laravel can not do this where size IN ()
because it will be syntax error (you basically say give me all rows that match this size, but you do not pass size). So in case the array is empty it just puts 0 = 1
.
为了告诉 Laravel,如果没有传入 size,不添加 size 条件,只需在此之前简单检查一下即可.
In order to tell Laravel, if no size is passed, to not add condition for size just put a simple check before that.
$product = new Product;
if (!empty($sizes)) {
$product = $product->whereIn('size', $sizes);
}
$products = $product->get();
顺便说一句,这种行为是一个修补程序.在以前的 Laravel 版本中,如果你传递空数组,你只会因为语法错误而抛出异常.现在只需设置 1 = 0
Btw this behaviour is a hotfix. In previous versions of Laravel, if you pass empty array you just had an exception thrown for a syntax error. Now it is handled by just setting 1 = 0
这篇关于多在何处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多在何处
基础教程推荐
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01