Mysql LAravel查询生成器-在何处查询有点不同
我有一个像这样的项目表格- 我想选择公司ID为0和2的所有数据(公司ID可能为1,3,3,4,5,6,…) 所以,我在Laravel查询生成器中所做的是-Mysql LAravel查询生成器-在何处查询有点不同,mysql,laravel,laravel-4,laravel-5,query-builder,Mysql,Laravel,Laravel 4,Laravel 5,Query Builder,我有一个像这样的项目表格- 我想选择公司ID为0和2的所有数据(公司ID可能为1,3,3,4,5,6,…) 所以,我在Laravel查询生成器中所做的是- DB::table( 'project') ->where('project.is_archived', '=', 0) ->where('project.companyID', '=', 0) ->orwhere('project.companyID', '=', 2)
DB::table( 'project')
->where('project.is_archived', '=', 0)
->where('project.companyID', '=', 0)
->orwhere('project.companyID', '=', 2)
->get;
但它不起作用
有人能帮忙吗
DB::table('project')
->where('is_archived', 0)
->whereIn('companyID', [0, 2])
->get();
这应该是可行的:)除了mimo的解决方案之外,您还可以使用回调或特殊的
where
方法组合语句
比如:
DB::table('project')
->where('is_archived', 0)
->orWhere(function(Builder $query) {
$query
->where('companyID', 0)
->where('companyID', 2)
});
将创建以下语句:
SELECT * FROM project WHERE (is_archived = 0) AND (companyID = 0 OR companyID = 2)
您还可以使用一个简单的->where(function($query){})
将封装的where()
语句的$boolean
参数更改为'or'(这可以允许更复杂的语句)
有关详细信息,请参阅Laravel 5.1文档