Laravel postgres sql 不区分大小写喜欢
Laravel postgres sql Case Insensitive Like
我在 Laravel 中有一个 postgres sql 查询:
$_query = Article::join('users', 'articles.user_id', '=', 'users.id')
->select('users.*','articles.*');
if( array_key_exists('title', $parameters) && $parameters['title'] != '' )
$_query->whereRaw( " LOWER(nbl_region_ref.region) like LOWER('%?%')", array( trim($parameters['region']) ) );
$result = $_query->get();
Output/Error:'PDOException' 消息 'SQLSTATE[42P18]: Indeterminate datatype: 7 ERROR: could not determine data type of parameter '
尝试过查询生成器:
$_query= DB::select("select users.*, articles.* from articles")
->join('users', 'articles.user_id', '=', 'users.id');
if( array_key_exists('title', $parameters) && $parameters['title'] != '' )
$_query->where( "articles.title","ILIKE", array( trim($parameters['title']) ) );
$result = $_query->get();
输出:无效的 FROM..table 未找到
试过 ILike(基于没有连接的类似问题)
$_query = Article::join('users', 'articles.user_id', '=', 'users.id')
->select('users.*','articles.*');
if( array_key_exists('title', $parameters) && $parameters['title'] != '' )
$_query->where( "articles.title","ILIKE", array( trim($parameters['title']) ) );
输出:空数组
尝试过:
$_query = Article::join('users', 'articles.user_id', '=', 'users.id')
->select('users.*','articles.*');
$_query->where( function ( $_queryTemp ) use ( $parameters ) {
if( array_key_exists('title', $parameters) && $parameters['title'] != '' )
$_query->whereRaw( " LOWER(nbl_region_ref.region) like LOWER('%?%')", array( trim($parameters['region']) ) );
});
Output/Error:'PDOException' 消息 'SQLSTATE[42P18]: Indeterminate datatype: 7 ERROR: could not determine data type of parameter '
我必须根据输入参数进行不区分大小写的搜索查询。
您的第三次尝试符合您的要求。根据您的第一次和最后一次尝试,您似乎希望搜索文本包含在“%”中。由于您第三次尝试没有这样做,我假设这就是您的查询未找到任何结果(空数组)的原因。
查询应该是:
$_query = Article::join('users', 'articles.user_id', '=', 'users.id')
->select('users.*','articles.*');
if (array_key_exists('title', $parameters) && $parameters['title'] != '') {
$_query->where('articles.title', 'ILIKE', '%'.trim($parameters['title']).'%');
}
使用 ILIKE
而不是 LIKE
为我解决了这个问题
与MySQL不同,PostgreSQL有两个模式匹配选项:ILIKE
和LIKE
。
ILIKE
用于 不区分大小写
LIKE
用于 区分大小写
如果你想根据不敏感的值进行查询,比如title
。你应该使用 ILIKE
.
// app/Models/Article.php
public static function getByTitle($title){
return self::where('title', 'ilike', "%${title}%")->orderBy('title', 'asc')->get();
}
有关 PostgreSQL 文档的更多信息:
The key word ILIKE can be used instead of LIKE to make the match
case-insensitive according to the active locale. This is not in the
SQL standard but is a PostgreSQL extension.
我在 Laravel 中有一个 postgres sql 查询:
$_query = Article::join('users', 'articles.user_id', '=', 'users.id')
->select('users.*','articles.*');
if( array_key_exists('title', $parameters) && $parameters['title'] != '' )
$_query->whereRaw( " LOWER(nbl_region_ref.region) like LOWER('%?%')", array( trim($parameters['region']) ) );
$result = $_query->get();
Output/Error:'PDOException' 消息 'SQLSTATE[42P18]: Indeterminate datatype: 7 ERROR: could not determine data type of parameter '
尝试过查询生成器:
$_query= DB::select("select users.*, articles.* from articles")
->join('users', 'articles.user_id', '=', 'users.id');
if( array_key_exists('title', $parameters) && $parameters['title'] != '' )
$_query->where( "articles.title","ILIKE", array( trim($parameters['title']) ) );
$result = $_query->get();
输出:无效的 FROM..table 未找到
试过 ILike(基于没有连接的类似问题)
$_query = Article::join('users', 'articles.user_id', '=', 'users.id')
->select('users.*','articles.*');
if( array_key_exists('title', $parameters) && $parameters['title'] != '' )
$_query->where( "articles.title","ILIKE", array( trim($parameters['title']) ) );
输出:空数组
尝试过:
$_query = Article::join('users', 'articles.user_id', '=', 'users.id')
->select('users.*','articles.*');
$_query->where( function ( $_queryTemp ) use ( $parameters ) {
if( array_key_exists('title', $parameters) && $parameters['title'] != '' )
$_query->whereRaw( " LOWER(nbl_region_ref.region) like LOWER('%?%')", array( trim($parameters['region']) ) );
});
Output/Error:'PDOException' 消息 'SQLSTATE[42P18]: Indeterminate datatype: 7 ERROR: could not determine data type of parameter '
我必须根据输入参数进行不区分大小写的搜索查询。
您的第三次尝试符合您的要求。根据您的第一次和最后一次尝试,您似乎希望搜索文本包含在“%”中。由于您第三次尝试没有这样做,我假设这就是您的查询未找到任何结果(空数组)的原因。
查询应该是:
$_query = Article::join('users', 'articles.user_id', '=', 'users.id')
->select('users.*','articles.*');
if (array_key_exists('title', $parameters) && $parameters['title'] != '') {
$_query->where('articles.title', 'ILIKE', '%'.trim($parameters['title']).'%');
}
使用 ILIKE
而不是 LIKE
为我解决了这个问题
与MySQL不同,PostgreSQL有两个模式匹配选项:ILIKE
和LIKE
。
ILIKE
用于 不区分大小写LIKE
用于 区分大小写
如果你想根据不敏感的值进行查询,比如title
。你应该使用 ILIKE
.
// app/Models/Article.php
public static function getByTitle($title){
return self::where('title', 'ilike', "%${title}%")->orderBy('title', 'asc')->get();
}
有关 PostgreSQL 文档的更多信息:
The key word ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. This is not in the SQL standard but is a PostgreSQL extension.