有没有办法在PostgreSQL中编写不区分大小写的查询,例如,我想下面3个查询返回相同的结果。

SELECT id FROM groups where name='administrator'

SELECT id FROM groups where name='ADMINISTRATOR'

SELECT id FROM groups where name='Administrator'

当前回答

您还可以阅读ILIKE关键字。尽管它不符合SQL标准,但有时它非常有用。更多信息请访问:http://www.postgresql.org/docs/9.2/static/functions-matching.html

其他回答

您还可以阅读ILIKE关键字。尽管它不符合SQL标准,但有时它非常有用。更多信息请访问:http://www.postgresql.org/docs/9.2/static/functions-matching.html

在比较之前,使用LOWER函数将字符串转换为小写。

试试这个:

SELECT id 
  FROM groups
 WHERE LOWER(name)=LOWER('Administrator')
select id from groups where name in ('administrator', 'ADMINISTRATOR', 'Administrator')

对于不区分大小写的参数化查询,您可以使用以下语法:

 "select * from article where upper(content) LIKE upper('%' || $1 || '%')"

您还可以使用POSIX正则表达式,例如

SELECT id FROM groups where name ~* 'administrator'

SELECT 'asd' ~* 'asd'返回t