1、一種方法是可以設定表或行的collation,使其為binary或case sensitive。在mysql中,對於column collate其約定的命名方法如下:
*_bin: 表示的是binary case sensitive collation,也就是說是區分大小寫的
*_cs: case sensitive collation,區分大小寫
*_ci: case insensitive collation,不區分大小寫
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)
2、另外一種方法
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)
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)
要讓mysql查詢區分大小寫,可以:
select * from table_name where binary a like 'a%'
select * from table_name where binary a like 'a%'
也可以在建表時,加以標識
create table table_name(
a varchar (20) binary
)
MySQL模糊查詢 like 時區分大小寫
問題說明 通過上面的語句,你會發現mysql的like查詢是不區分大小寫的,因為我的失誤,把joe寫成了joe才發現了這個東東吧。但是,有時候,我們需要區分大小寫的是,該怎麼辦呢?解決方法如下 方法一 查詢時,指定區分大小寫 很簡單,在like的後面加個binary就可以了,適用於表的結構不易改變的...
MySQL模糊查詢(like)時區分大小寫
問題說明 通過上面的語句,你會發現mysql的like查詢是不區分大小寫的,因為我的失誤,把joe寫成了joe才發現了這個東東吧。但是,有時候,我們需要區分大小寫的是,該怎麼辦呢?解決方法如下 方法一 查詢時,指定區分大小寫 很簡單,在like的後面加個binary就可以了,適用於表的結構不易改變的...
mysql模糊查詢like優化
使用 like value 不走索引 select column from table where field like keyword 1.可使用 like value 前值匹配,可走索引 select column from table where field like keyword 2.使用...