简体   繁体   中英

SELECT with lower case + unaccent + multiple columns in PostgreSQL

Table schools

id | address                 | name
1  | Rybničná 59, Bratislava | Stredná odborná škola elektrotechnická
2  | Ul. Sibírska 1, Trnava  | Stredná odborná škola elektrotechnická

What I want

From client If I want to type:

  1. Stredná odborná
  2. stredná odborná
  3. stredna odborna

It must find rows with id 1 and 2

If I want to type Bratislava or bratis It must find row with id 1

What I have

SELECT * FROM schools WHERE unaccent(address) LIKE ('%' || 'bratis' || '%');

I need to select from 2 columns (address and name)

I hope this works

  SELECT * FROM schools 
  WHERE unaccent(address|| ' ' ||name) ILIKE ('%' || 'bratis' || '%');

To make the search case insentive , use ILIKE instead of LIKE . Then, you would want to remove the accents from the input string as well. At last, just use AND or OR to combine the two criteria (note that you could use the same search term for both columns - use OR in this case)

SELECT * FROM schools 
WHERE unaccent(address) ILIKE ('%' || unaccent('bratis') || '%')
  AND unaccent(name) ILIKE ('%' || unaccent('Stredná odborná') || '%')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM