我在 laravel 中有这段代码来获取即将用完的产品。
$productos = DB::table('productos')
->where('producto_minimo', '>=', 'producto_cantidad')
->get();
我得到的是以下结果
这不是正确的结果。在 MySql 中,通过此查询 SELECT * FROM productos where producto_minimo >= producto_cantidad;
更新
查询日志 - DB::getQueryLog()
- 显示了这一点
2 =>
array (size=3)
'query' => string 'select * from `productos` where `producto_minimo` >= ?' (length=54)
'bindings' =>
array (size=1)
0 => string 'producto_cantidad' (length=17)
'time' => float 1
最佳答案
我假设您必须使用 whereRaw
方法:
$productos = DB::table('productos')
->whereRaw('producto_minimo >= producto_cantidad')
->get();
您的查询会将 producto_minimo
列中的值与字符串 'producto_cantidad' 进行比较
查看 advanced wheres 的 Eloquents 文档:
DB::table('users')
->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
The query above will produce the following SQL:
select * from users
where exists (
select 1 from orders where orders.user_id = users.id
)
关于php - 为什么 laravel 查询不返回正确的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25122554/