SQL'LIKE'语法 [英] SQL 'LIKE' syntax
问题描述
我正在编写一个需要同时在mysql和postgresql上运行的应用程序。
我必须使用 like
来比较一些值。
I'm writing an application that needs to work on both mysql and postgresql.
I have to use like
to compare some values.
在mysql 中就像
一样,不区分大小写。
在PostgreSQL Like
中区分大小写,在 ILIKE
中不区分大小写。
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 ?
PDO对此有解决方案吗?
Does PDO have a solution for this ?
推荐答案
最简单的方法确保不区分大小写的LIKE的方法是使用以下类似方法之一:
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)
或者您可以大写/小写-在SQL外部设置模式
,然后使用:
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
我倾向于使用 LOWER
出于习惯,因为小写字母更易于阅读,因此更易于调试。
I tend to use LOWER
out of habit as lower case is easier to read and hence easier to debug.
这篇关于SQL'LIKE'语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!