SQLite區分大小寫查詢

2022-08-09 03:15:07 字數 1494 閱讀 4025

大部分資料庫在進行字串比較的時候,對大小寫是不敏感的。

但是,在sqlite中,對大小寫是敏感的。

假設表test的結構和值如下:

_id

name

1abcde

2abcde

3abcde

4abcde

5aaaaa

6bbbbb

執行下面的sql語句:

select * from test where name = 'abcde';

結果是沒有查詢到任何記錄。

明顯地,sqlite在進行字串比較的時候,預設對大小寫是敏感的

那麼sqlite怎麼區分大小寫查詢呢,以下是三種解決方案:

方案一:使用大小寫轉換函式lower、upper

1.select * from test where lower(name) = 'abcde';

2.select * from test where lower(name) = lower('abcde');

3.select * from test where lower(name) = lower('abcde');

4.select * from test where lower(name) = lower('abcde');

....

(1).select * from test where upper(name) = 'abcde';

(2).select * from test where upper(name) = upper('abcde');

(3).select * from test where upper(name) = upper('abcde');

(4).select * from test where upper(name) = upper('abcde');

.....

查詢到的記錄都如下:

1abcde

2abcde

3abcde

4abcde

方案二:在進行比較時強制宣告不區分大小寫

select * from test where name = 'abcde'collate nocase;

查詢到的記錄如下:

1abcde

2abcde

3abcde

4abcde

方案三:建立表時宣告該字段不區分大小寫

create table test (_id integer,name textcollate nocase);

如果在任何情況下都不需要對大小寫敏感,方案三是最好的解決方案;

如果只是少量查詢對大小寫不敏感,可以用方案二。

而方案一由於用到了函式,可能有額外的效能消耗,不推薦使用。

SQLite區分大小寫查詢

大部分資料庫在進行字串比較的時候,對大小寫是不敏感的。但是,在sqlite中,對大小寫是敏感的。假設表test的結構和值如下 idname 1abcde 2abcde 3abcde 4abcde 5aaaaa 6bbbbb 執行下面的sql語句 select from test where name...

SQLite區分大小寫查詢

大部分資料庫在進行字串比較的時候,對大小寫是不敏感的。但是,在sqlite中,對大小寫是敏感的。假設表test的結構和值如下 id name 1abcde 2abcde 3abcde 4abcde 5aaaaa 6bbbbb 執行下面的sql語句 select from test where nam...

Sqlite 模糊查詢 區分大小寫

pragma case sensitive like on pragma case sensitive like off select from test where destid 12345 and content like ab string whereclause id and age and...