Lumen8中Eloquent Where查询返回错误结果及配置差异排查
Laravel/Lumen 8.3.4 查询Oracle数据库结果异常
环境与数据库数据
使用Laravel/Lumen 8.3.4版本,Oracle数据库中存在以下4条数据:
| ID | FOREIGN_ID | NAME |
|---|---|---|
| 1 | 100 | Entry1 |
| 2 | 100 | Entry2 |
| 3 | 100 | Entry3 |
| 4 | 200 | Entry4 |
模型代码
Entry模型代码如下:
class Entry extends Model { protected $connection = 'MyConnection'; protected $table = 'MY_TABLE'; protected $fillable = ['foreign_id', 'name']; protected $hidden = ['foreign_id']; protected $casts = [ 'foreign_id' => 'integer' ]; }
查询异常现象
执行以下查询时,预期返回空集合,但实际只返回ID为1的Entry1:
Entry::where([['id', '!=', 1], 'foreign_id' => 100, 'name' => 'Entry1'])->get();
分步添加Where条件测试:
- 以下查询返回除Entry1外的所有数据,结果正确:
Entry::where(['id', '!=', 1])->get(); - 以下查询返回Entry2和Entry3,结果正确:
Entry::where(['id', '!=', 1])->where(['foreign_id' => 100])->get(); - 以下查询只返回Entry1,结果错误(预期为空集合):
Entry::where(['id', '!=', 1])->where(['foreign_id' => 100])->where(['name' => 'Entry1'])->get();
生成的SQL语句
查询生成的SQL语句为:
"select * from "MY_TABLE" where ("ID" != ? and "FOREIGN_ID" = ? and "NAME" = ?)"
疑问
在另一台电脑上执行该查询返回空集合,将!=替换为not like后问题解决,请问是什么配置差异导致了这种情况?
内容的提问来源于stack exchange,提问作者Febertson
相关产品推荐
相关产品推荐

