[英]Laravel query builder raw AND statement
I need to run a raw SQL query part in Laravel.我需要在 Laravel 中运行原始 SQL 查询部分。
The raw SQL would look basically like this:原始 SQL 基本上如下所示:
AND (forretningsadresse_fylke = 'HEDMARK'
AND (forretningsadresse_kommune = 'HAMAR')
OR (forretningsadresse_kommune = 'ELVERUM')
)
OR (forretningsadresse_fylke = 'HORDALAND'
AND (forretningsadresse_kommune = 'BERGEN')
)
I try to use:我尝试使用:
$result = Company::where(function($a) use($input, $sql) {
// Name or keywords LIKE string
$a -> where('navn', 'LIKE', '%'. $input['query_string'] .'%');
$a -> where('keywords', 'LIKE', '%'. $input['query_string'] .'%');
if (!empty($sql)) {
$a -> where(DB::statement($sql));
}
});
$result = $result -> take($input['limit']) // Take $limit
-> offset($input['offset']) // Offset by $offset
-> orderBy('rank', 'DESC') // Sponsors first
-> get();
But it doesn't work.但它不起作用。 See the error below.
请参阅下面的错误。 How would I get this to work?
我如何让这个工作? Thanks in advance!
提前致谢!
QueryException in Connection.php line 673: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; Connection.php 第 673 行中的 QueryException:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AND (forretningsadresse_fylke = 'AKERSHUS')' at line 1 (SQL: AND (forretningsadresse_fylke = 'AKERSHUS') )
检查与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行的“AND (forretningsadresse_fylke = 'AKERSHUS')' 附近使用的正确语法(SQL: AND (forretningsadresse_fylke = 'AKERSHUS'))
EDIT: The table has 1 million rows, and I use this method to create the raw query: http://i.imgur.com/Q5SRP58.png编辑:该表有 100 万行,我使用此方法创建原始查询: http : //i.imgur.com/Q5SRP58.png
There are two issues with your code:您的代码有两个问题:
DB::statement
where you should be using DB::raw
.DB::raw
地方使用DB::statement
。 DB::statement
will execute the string that you pass as an argument and that is not what you need. DB::statement
将执行您作为参数传递的字符串,这不是您需要的。 You need to pass a raw statement to be included to the query builder.$sql
parameter, then you need to remove the starting AND
, otherwise the query will end up with .... and AND (
, which will throw an error.$sql
参数,那么您需要删除起始AND
,否则查询将以.... and AND (
,这将引发错误。 Given this, you need to update your code to:鉴于此,您需要将代码更新为:
// Notice the removal of the `AND`
$sql = "(forretningsadresse_fylke = 'HEDMARK'
AND (forretningsadresse_kommune = 'HAMAR')
OR (forretningsadresse_kommune = 'ELVERUM')
)
OR (forretningsadresse_fylke = 'HORDALAND'
AND (forretningsadresse_kommune = 'BERGEN')
)";
$result = Company::where(function($a) use($input, $sql) {
// Name or keywords LIKE string
$a->where('navn', 'LIKE', '%'. $input['query_string'] .'%');
$a->where('keywords', 'LIKE', '%'. $input['query_string'] .'%');
if (!empty($sql)) {
// Using DB::raw instead of DB::statement
$a->where(DB::raw($sql));
}
});
Try尝试
$result = Company::where('navn', 'LIKE', '%'. $input['query_string'] .'%')
->where('keywords', 'LIKE', '%'. $input['query_string'] .'%')
->where(function($query){
$query->where('forretningsadresse_fylke', '=', 'HEDMARK' )
->where('forretningsadresse_kommune', '=', 'HAMAR')
->orWhere('forretningsadresse_kommune','=', 'ELVERUM');
})
->where(function($query){
$query->orWhere('forretningsadresse_fylke','=', 'HORDALAND')
->where('forretningsadresse_kommune', '=', 'BERGEN');
})
->take($input['limit']) // Take $limit
->offset($input['offset']) // Offset by $offset
->orderBy('rank', 'DESC') // Sponsors first
->get();
hope it helps希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.