MySQL查詢時區分大小寫的方法

2022-03-19 03:31:56 字數 4396 閱讀 5541

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)

要讓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

查詢時區分大小寫

生成測試資料 setansi nulls ongo setquoted identifier ongo setansi padding ongo create table dbo test name varchar 20 null,quarter int null,profile int null,...

MySQL查詢時區分大小寫的方法

mysql查詢預設是不區分大小寫的 如 select from table name where a like a select from table name where a like a select from table name where a like a select from tabl...

MySQL查詢時區分大小寫的方法

mysql查詢預設是不區分大小寫的 如 select from table name where a like a select from table name where a like a select from table name where a like a select from tabl...