使MySQL查詢區分大小寫(轉)
1、一種方法是可以設定表或行的collation,使其為binary或case sensitive。在MySQL中,對於Column Collate其約定的命名方法如下:
*_bin: 表示的是binary case sensitive collation,也就是說是區分大小寫的
*_cs: case sensitive collation,區分大小寫
*_ci: case insensitive collation,不區分大小寫
###########
# Start binary collation example
###########
mysql> create table case_bin_test (word VARCHAR(10)) CHARACTER SET latin1 COLLATE latin1_bin;
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO case_bin_test VALUES ('Frank'),('Google'),('froogle'),('flickr'),('FlicKr');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM case_bin_test WHERE word LIKE 'f%';
+---------+
| word |
+---------+
| froogle |
| flickr |
+---------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM case_bin_test WHERE word LIKE 'F%';
+---------+
| word |
+---------+
| Frank |
| FlicKr |
+---------+
4 rows in set (0.00 sec)
###########
# End
###########
2、另外一種方法
###########
# Start case sensitive collation example
###########
mysql> create table case_cs_test (word VARCHAR(10)) CHARACTER SET latin1 COLLATE latin1_general_cs;
Query OK, 0 rows affected (0.08 sec)
mysql> INSERT INTO case_cs_test VALUES ('Frank'),('Google'),('froogle'),('flickr'),('FlicKr');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM case_cs_test WHERE word LIKE 'F%';
+---------+
| word |
+---------+
| Frank |
| FlicKr |
+---------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM case_cs_test WHERE word LIKE 'f%';
+---------+
| word |
+---------+
| froogle |
| flickr |
+---------+
2 rows in set (0.00 sec)
###########
# end
###########
3、還有一種方法就是在查詢時指定collation
mysql> create table case_test (word VARCHAR(10)) CHARACTER SET latin1;
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO case_test VALUES ('Frank'),('Google'),('froogle'),('flickr'),('FlicKr');
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM case_test WHERE word LIKE 'f%';
+---------+
| word |
+---------+
| Frank |
| froogle |
| flickr |
| FlicKr |
+---------+
6 rows in set (0.01 sec)
mysql> SELECT * FROM case_test WHERE word LIKE 'F%';
+---------+
| word |
+---------+
| Frank |
| froogle |
| flickr |
| FlicKr |
+---------+
6 rows in set (0.01 sec)
mysql> SELECT * FROM case_test WHERE word COLLATE latin1_bin LIKE 'F%';
+---------+
| word |
+---------+
| Frank |
| FlicKr |
+---------+
4 rows in set (0.05 sec)
mysql> SELECT * FROM case_test WHERE word COLLATE latin1_bin LIKE 'f%';
+---------+
| word |
+---------+
| froogle |
| flickr |
+---------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM case_test WHERE word LIKE 'f%' COLLATE latin1_bin;
+---------+
| word |
+---------+
| froogle |
| flickr |
+---------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM case_test WHERE word LIKE 'F%' COLLATE latin1_bin;
+---------+
| word |
+---------+
| Frank |
| FlicKr |
+---------+
4 rows in set (0.01 sec)
mysql> SELECT * FROM case_test WHERE word LIKE 'F%' COLLATE latin1_general_cs;
+---------+
| word |
+---------+
| Frank |
| FlicKr |
+---------+
4 rows in set (0.04 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/21601207/viewspace-723135/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 模糊查詢區分大小寫嗎?
- Mysql模糊查詢預設是不區分字母大小寫的MySql
- 配置CaseInsensitiveMap使key不區分大小寫
- MySQL不區分大小寫設定MySql
- 關於sqlserver字元型別查詢條件區分大小寫SQLServer字元型別
- 區分大小寫
- Git 區分大小寫Git
- MySQL表名不區分大小寫的設定方法MySql
- 查詢MySQL資料庫,MySQL表的大小MySql資料庫
- mongodb如何不區分大小寫MongoDB
- MySQL的where條件字串區分大小寫的問題MySql字串
- python中不區分大小寫嗎Python
- Nginx支援url不區分大小寫Nginx
- MySQL的分頁查詢MySql
- mysql-分組查詢-子查詢-連線查詢-組合查詢MySql
- MySQL分頁查詢優化MySql優化
- MySQL——優化巢狀查詢和分頁查詢MySql優化巢狀
- 帶你玩玩轉 MySQL 查詢MySql
- MySQL-資料記錄大小寫轉換MySql
- 二分查詢 | 二分查詢的一種推薦寫法
- Python實用技法第25篇:正則:以不區分大小寫的方式對文字做查詢和替換Python
- MySQL字串轉時間戳查詢MySql字串時間戳
- 【SQL】Oracle查詢轉換之物化檢視查詢重寫SQLOracle
- 最新版idea不區分大小寫設定Idea
- Git中如何設定區分檔名大小寫Git
- 區分關聯子查詢和非關聯子查詢
- 查詢資料庫大小資料庫
- 查詢資料量的大小
- [玩轉MySQL之六]MySQL查詢優化器MySql優化
- MYSQL中怎麼查詢LONGBLOB型別資料的大小MySql型別
- MySQL分優化之超大頁查詢MySql優化
- (MySQL學習筆記)分頁查詢MySql筆記
- 字串-大小寫轉換字串
- [20220826]ls顯示檔案不區分大小寫.txt
- 查詢——二分查詢
- mysql加強(3)~分組(統計)查詢MySql
- MySQL查詢MySql
- MySQL like查詢字元轉義遇到的坑MySql字元
- Excel資料庫轉MySQL,實現查詢Excel資料庫MySql