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

SELECT id FROM groups where name='administrator'

SELECT id FROM groups where name='ADMINISTRATOR'

SELECT id FROM groups where name='Administrator'

当前回答

-- Install 'Case Ignore Test Extension'
create extension citext;

-- Make a request
select 'Thomas'::citext in ('thomas', 'tiago');

select name from users where name::citext in ('thomas', 'tiago');

其他回答

你可以用ILIKE。即。

SELECT id FROM groups where name ILIKE 'administrator'

我喜欢在这种情况下工作:

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

使用~*可以通过INSTR的功能极大地提高性能。

SELECT id FROM groups WHERE name ~* 'adm'

返回name包含OR = 'adm'的行。

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