1

对于搜索查询,我有以下内容:

DB::whereRaw('column = ?', 'foo')->orWhereRaw('column IS NULL')->get();

添加orWhereRaw语句给我的结果比仅whereRaw. 添加另一个时,它似乎以某种方式忽略了第一个。它包含在 SQL 语句中。还有另一种比较字符串和空值的方法吗?

我还尝试了以下方法,如下所示:

return self::select('id')
    ->where('current_state', 'unavailable') 
    ->orWhereNull('current_state') 
    ->get();

如果我更改顺序(首先是 whereNull,其次是 whereNull),这也会给我不同的结果。与 where 子句相对应的包容性查询似乎无法正常运行。如果我使用常规的 where 子句,我不会遇到任何问题。

跑步SELECT * FROM events WHERE current_state='unavailable' OR current_state IS NULL;确实为我产生了正确的结果。

4

1 回答 1

3

不要使用 whereRaw 检查空值。您可以改用它:

->orWhereNull('column')

除非您正在做一些额外的事情,例如 mysql 函数,否则执行第一个 where 的正确方法就是像这样传递列:

where('column', '=', 'foo') 

您实际上可以消除等号,因为它默认为。所以你的查询是:

DB::table('table')->where('column', 'foo')->orWhereNull('column')->get();
于 2017-01-23T21:42:37.483 回答