Home >Database >Mysql Tutorial >How Can I Perform Accent-Insensitive Queries in PostgreSQL?
Microsoft SQL Server provides an "accent-insensitive" collation, allowing a query like SELECT * FROM users WHERE name LIKE 'João'
to be executed to retrieve rows with the name "Joao".
PostgreSQL provides the unaccent module, which removes diacritics from strings. To use it, install the extension and use the unaccent()
function:
<code class="language-sql">SELECT * FROM users WHERE unaccent(name) = unaccent('João');</code>
To speed up queries, create an expression index:
<code class="language-sql">CREATE INDEX users_unaccent_name_idx ON users(unaccent(name));</code>
Remember to qualify functions and dictionaries in mode for safety reasons:
<code class="language-sql">CREATE OR REPLACE FUNCTION public.f_unaccent(text) RETURNS text LANGUAGE sql IMMUTABLE PARALLEL SAFE STRICT AS $func$ SELECT public.immutable_unaccent('public.unaccent', ) -- 限定函数和字典的模式 $func$; CREATE INDEX users_unaccent_name_idx ON users(public.f_unaccent(name));</code>
In PostgreSQL 9.6 and later, unaccent()
correctly expands ligatures, converting "Œ" to "OE" and "ß" to "ss".
Using unaccent
in conjunction with the pg_trgm
module allows you to perform arbitrary pattern matching using LIKE
or ILIKE
and create a triplet GIN or GIST expression index.
For simplicity, consider using triplet indexing for left-anchored patterns.
The above is the detailed content of How Can I Perform Accent-Insensitive Queries in PostgreSQL?. For more information, please follow other related articles on the PHP Chinese website!