猿问

Laravel 查询或运算符优先级

我正在使用 laravel 4 查询。我想通过 OR 条件获取数据。Laravel 4 是否有任何优先级规则?


我的查询是:


SELECT * 

FROM order_items 

WHERE (status = 'Y' OR (status = 'N' AND amount > 100)) 

AND product_id = '15' 

LIMIT 1;

我有 2 个订单项目,但我希望结果中的第一个项目将获得优先状态 = 'Y',如果它不存在,则它满足 OR 的第二个条件。


这是我的 Laravel 代码:


$sql_select = "SELECT * 

               FROM order_items 

                WHERE (status = 'Y' OR (status = 'N' AND amount > 100)) 

                AND product_id = '15' 

                LIMIT 1";

$data = DB::select($sql_select);

echo "<pre>";

print_r($data);

die;

对于简单的 mysql 查询,它可以工作,但在 laravel 中它没有给出结果。


冉冉说
浏览 143回答 2
2回答

白衣非少年

也许你应该试试这种方式DB::table('order_items')&nbsp; &nbsp; &nbsp; &nbsp; ->where('status', 'Y')&nbsp; &nbsp; &nbsp; &nbsp; ->orWhere(function($query)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->where('status', 'N')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->where('amount', '>', '100');&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; ->where('product_id', 15)&nbsp; &nbsp; &nbsp; &nbsp; ->first();

www说

伟大的解决方案 andreeab。在我的情况下,SQL 查询是:SELECT ... WHERE c1 = 'xyz' AND (c2 = 100 OR c2 = 200);我在 Eloquent 中写道:&nbsp;DB:table('tablename')&nbsp; &nbsp; ->where('c1', '=', 'xyz')&nbsp; &nbsp; ->where(function($query)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;->where('c2', '=', 100)&nbsp; &nbsp; &nbsp; &nbsp;->orWhere('c2', '=', 200);&nbsp; &nbsp; })&nbsp; &nbsp; ->get();
随时随地看视频慕课网APP
我要回答