Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL 'LIKE' syntax

I'm writing an application that needs to work on both mysql and postgresql. I have to use like to compare some values.

In mysql LIKE it's case insensitive. In postgresql LIKE it's case sensitive and ILIKE it's case insensitive.

What is the best way to make a solid query that works on both in case the match has to be case insensitive ?

Does PDO have a solution for this ?

like image 936
johnlemon Avatar asked Jan 29 '11 16:01

johnlemon


2 Answers

The easiest way to ensure a case-insensitive LIKE is to use something like one of these:

LOWER(column_name) LIKE LOWER(pattern)
UPPER(column_name) LIKE UPPER(pattern)

Or you can up-case/down-case the pattern outside the SQL and just use:

LOWER(column_name) LIKE down_cased_pattern
UPPER(column_name) LIKE up_cased_pattern

I tend to use LOWER out of habit as lower case is easier to read and hence easier to debug.

like image 62
mu is too short Avatar answered Oct 20 '22 12:10

mu is too short


This is the job of a Database Abstraction Layer.

PDO does not do what you're looking for but there are a few PHP DALs if you google for them.

PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility.

like image 40
Jeff Swensen Avatar answered Oct 20 '22 13:10

Jeff Swensen