Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using LOWER with IN condition [duplicate]

Tags:

postgresql

assume I have a table named comodity_group and the structure looks like:

+----------+-------+
| group_id | name  |
+----------+-------+
| 1        | Data1 |
+----------+-------+
| 2        | Data2 |
+----------+-------+
| 3        | data3 |
+----------+-------+

and I have the following query

SELECT * FROM comodity_group WHERE name IN('data1','data2','data3')

the query return 0 result, because condition is all in lowercase (note that the condition is also dynamic, meaning it can be Data1 or daTa1, etc)

so I want to make both condition and field name in lowercase, in other word case insensitive.

like image 857
Dariel Pratama Avatar asked Feb 05 '23 07:02

Dariel Pratama


1 Answers

You can use ILIKE and an array:

select *
from comodity_group
where name ilike any (array['Data1', 'data2', 'dATA3']);

Note that this won't be really fast as the ILIKE operator can't make use of a regular index on the name column.

like image 59
a_horse_with_no_name Avatar answered Feb 15 '23 09:02

a_horse_with_no_name