有没有办法在PostgreSQL中编写不区分大小写的查询,例如,我想下面3个查询返回相同的结果。
SELECT id FROM groups where name='administrator'
SELECT id FROM groups where name='ADMINISTRATOR'
SELECT id FROM groups where name='Administrator'
有没有办法在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');
其他回答
-- 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关键字。尽管它不符合SQL标准,但有时它非常有用。更多信息请访问:http://www.postgresql.org/docs/9.2/static/functions-matching.html
对于不区分大小写的参数化查询,您可以使用以下语法:
"select * from article where upper(content) LIKE upper('%' || $1 || '%')"
限制使用ILIKE
select id from groups where name ILIKE 'adminstration';
如果你来,expressjs的背景和名称是一个变量 使用
select id from groups where name ILIKE $1;
我喜欢在这种情况下工作:
SELECT id
FROM groups
WHERE name ILIKE 'Administrator'