我正在使用 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 中它沒有給出結果。
2 回答

白衣非少年
TA貢獻1155條經驗 獲得超0個贊
也許你應該試試這種方式
DB::table('order_items')
->where('status', 'Y')
->orWhere(function($query)
{
$query->where('status', 'N')
->where('amount', '>', '100');
})
->where('product_id', 15)
->first();

www說
TA貢獻1775條經驗 獲得超8個贊
偉大的解決方案 andreeab。
在我的情況下,SQL 查詢是:
SELECT ... WHERE c1 = 'xyz' AND (c2 = 100 OR c2 = 200);
我在 Eloquent 中寫道:
DB:table('tablename')
->where('c1', '=', 'xyz')
->where(function($query)
{
->where('c2', '=', 100)
->orWhere('c2', '=', 200);
})
->get();
- 2 回答
- 0 關注
- 157 瀏覽
添加回答
舉報
0/150
提交
取消