区分大小写,如 select SQLite

Case sensitive like select SQLite

所以我有以下查询:

select username from users where username like 'aDam%'

但这里的问题是它不区分大小写。此查询还将获得名为 'adam%'、'Adam%' 等的用户。

问题:如何只获取'aDam%'(使查询区分大小写)?

注意:% 运算符在这里很重要。

由于 Like 不区分大小写,因此它不能单独使用,但一种解决方法是将它与 SUBSTR 函数一起使用

select username 
from users 
where username like 'aDam%' 
  and substr(username,1,4) = 'aDam'

在 运行 查询之前,您可以将 case_sensitive_like pragma 设置为 ON。

PRAGMA case_sensitive_like = boolean;

The default behavior of the LIKE operator is to ignore case for ASCII characters. Hence, by default 'a' LIKE 'A' is true. The case_sensitive_like pragma installs a new application-defined LIKE function that is either case sensitive or insensitive depending on the value of the case_sensitive_like pragma. When case_sensitive_like is disabled, the default LIKE behavior is expressed. When case_sensitive_like is enabled, case becomes significant. So, for example, 'a' LIKE 'A' is false but 'a' LIKE 'a' is still true.

This pragma uses sqlite3_create_function() to overload the LIKE and GLOB functions, which may override previous implementations of LIKE and GLOB registered by the application. This pragma only changes the behavior of the SQL LIKE operator. It does not change the behavior of the sqlite3_strlike() C-language interface, which is always case insensitive.

然后直接 运行 查询。

PRAGMA case_sensitive_like=ON;
select username from users where username like 'aDam%';

在 Sql Fiddle 上测试 here