1. 程式人生 > >【MYSQL】查詢不區分大小寫

【MYSQL】查詢不區分大小寫

MYSQL在預設的情況下查詢是不區分大小寫的,例如:

mysql> create table t1(
    -> name varchar(10));
Query OK, 0 rows affected (0.09 sec)

mysql> insert into t1 values('you'),('You'),('YOU');
Query OK, 3 rows affected (0.05 sec)
Records: 3  Duplicates: 0  Warnings: 0
對這個表,預設情況下,下面兩個查詢的結果是一樣的:

mysql> select * from t1 where name = 'you';

+------+
| name |
+------+
| you  |
| You  |
| YOU  |
+------+
3 rows in set (0.00 sec)

mysql> select * from t1 where name = 'YOU';
+------+
| name |
+------+
| you  |
| You  |
| YOU  |
+------+
3 rows in set (0.00 sec)

如果想讓MYSQL知道你輸入的字母是大寫還是小寫的,修改表:

mysql> alter table t1 change name name varchar(10) binary;
Query OK, 3 rows affected (0.20 sec)


Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t1 where name = 'you';
+------+
| name |
+------+
| you  |
+------+
1 row in set (0.00 sec)

mysql> select * from t1 where name = 'YOU';
+------+
| name |
+------+
| YOU  |
+------+
1 row in set (0.00 sec)

如果你只是想在SQL語句中實現的話:

mysql> select * from t1 where name = binary 'YOU';


+------+
| name |
+------+
| YOU  |
+------+
1 row in set (0.02 sec)

mysql> select * from t1 where name = binary 'you';
+------+
| name |
+------+
| you  |
+------+
1 row in set (0.00 sec)

如果不想這麼麻煩而想服務一開啟就讓大小寫一致的話:
可以修改my.ini或者my.cnf
[mysqld]
 lower_case_table_names=1
(0:區分;1:不區分)
然後重啟MYSQL服務。

mysql> show variables like '%case_table%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| lower_case_table_names | 1     |
+------------------------+-------+
1 row in set (0.00 sec)

注:WINDOWS系統不用修改,系統預設就是1
LINUX 系統預設是0。因為LINUX下的指令碼都是區分大小寫的。 

http://www.blogjava.net/brock/archive/2009/04/17/266168.html

歡迎關注公眾號: