標籤:
在MySQL中,NULL欄位的處理,需要注意,當在處理查詢條件中有NULL,很有可能你得到的值不是想要的,因為,在MySQL中,判斷NULL值相等(=)或者不等(!=)都會返回false。主要出現在常見的SELECT以及WHERE字句中。
為了處理這種特殊的情況,MySQL提供了如下的關鍵字進行特殊處理:
- IS NULL: 當列的值是NULL,此運算子返回true。
- IS NOT NULL: 當列的值不為NULL, 運算子返回true。
- <=>: 比較操作符(不同於=運算子),當比較的的兩個值為NULL時返回true。
關於 NULL 的條件比較運算是比較特殊的。你不能使用 = NULL 或 != NULL 在列中尋找 NULL 值 。
在MySQL中,NULL值與任何其它值的比較(即使是NULL)永遠返回false,即 NULL = NULL 返回false 。
下面看看例子,就很清楚的理解是什麼意思了。
先在test資料庫中建立一個表checknull。
1 mysql> use test2 Database changed3 mysql> show tables;4 Empty set (0.00 sec)5 6 mysql> create table checknull(7 -> name varchar(30) not null,8 -> age int);9 Query OK, 0 rows affected (0.11 sec)
我們看看這個表的建立基本資料,用show和desc分別查看:
1 mysql> show create table checknull; 2 +-----------+-------------------------------------------------------------------------------------------------------------------------------+ 3 | Table | Create Table | 4 +-----------+-------------------------------------------------------------------------------------------------------------------------------+ 5 | checknull | CREATE TABLE `checknull` ( 6 `name` varchar(30) NOT NULL, 7 `age` int(11) DEFAULT NULL 8 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 | 9 +-----------+-------------------------------------------------------------------------------------------------------------------------------+10 1 row in set (0.01 sec)11 12 mysql> desc checknull;13 +-------+-------------+------+-----+---------+-------+14 | Field | Type | Null | Key | Default | Extra |15 +-------+-------------+------+-----+---------+-------+16 | name | varchar(30) | NO | | NULL | |17 | age | int(11) | YES | | NULL | |18 +-------+-------------+------+-----+---------+-------+19 2 rows in set (0.00 sec)
我們開始測試一下,第一步,向這個表中插入資料:
1 mysql> insert checknull (name, age) values("water", 30); 2 Query OK, 1 row affected (0.00 sec) 3 4 mysql> 5 mysql> insert checknull (name, age) values("shihuc", NULL); 6 Query OK, 1 row affected (0.00 sec) 7 8 mysql> 9 mysql> select * from checknull;10 +--------+------+11 | name | age |12 +--------+------+13 | water | 30 |14 | shihuc | NULL |15 +--------+------+16 2 rows in set (0.00 sec)
接下來,再查詢看看,先查詢所有的age欄位是NULL的使用者資訊:
1 mysql> select * from checknull where age = NULL; 2 Empty set (0.00 sec) 3 4 mysql> select * from checknull where age IS NULL; 5 +--------+------+ 6 | name | age | 7 +--------+------+ 8 | shihuc | NULL | 9 +--------+------+10 1 row in set (0.00 sec)
1 mysql> select * from checknull where age != NULL; 2 Empty set (0.00 sec) 3 4 mysql> select * from checknull where age IS NOT NULL; 5 +-------+------+ 6 | name | age | 7 +-------+------+ 8 | water | 30 | 9 +-------+------+10 1 row in set (0.00 sec)
是不是發現,結果不同?那麼我現在,通過修改大小寫來查看查詢結果:
1 mysql> select * from checknull where age IS null; 2 +--------+------+ 3 | name | age | 4 +--------+------+ 5 | shihuc | NULL | 6 +--------+------+ 7 1 row in set (0.00 sec) 8 9 mysql> select * from checknull where age = null;10 Empty set (0.00 sec)
1 mysql> select * from checknull where age != null; 2 Empty set (0.00 sec) 3 4 mysql> select * from checknull where age is not null; 5 +-------+------+ 6 | name | age | 7 +-------+------+ 8 | water | 30 | 9 +-------+------+10 1 row in set (0.00 sec)
發現結果和上面的日誌反映的內容一樣。
這裡補充一個小tips,那就是在Linux下,mysql預設情況下,資料庫的名字,表的名字和欄位的名字是區分大小寫,但是欄位的值是不區分大小寫。
表的名字和欄位的名字是否區分大小寫,可以查看資料庫變數lower_case_table_names的值,0表示區分大小寫;1表示不區分,統一按照小寫對待。
1 mysql> show variables like "%case%";2 +------------------------+-------+3 | Variable_name | Value |4 +------------------------+-------+5 | lower_case_file_system | OFF |6 | lower_case_table_names | 0 |7 +------------------------+-------+8 2 rows in set (0.00 sec)
而對於欄位的值,想要區分大小寫,可以使用BINARY加以限制。不管是在建立表的時候,還是在查詢的條件字句中都可以使用。
1 mysql> create table lowupper( 2 -> name varchar(30) not null, 3 -> age int, 4 -> home varchar(40) binary); 5 Query OK, 0 rows affected (0.10 sec) 6 7 mysql> show create table lowupper; 8 +----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 9 | Table | Create Table |10 +----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+11 | lowupper | CREATE TABLE `lowupper` (12 `name` varchar(30) NOT NULL,13 `age` int(11) DEFAULT NULL,14 `home` varchar(40) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL15 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |16 +----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+17 1 row in set (0.00 sec)
上面日誌中,可以看到,建立表的時候,
在home欄位後面加了約束binary,通過show可以看到,home欄位有一個校對規則 latin1_bin,
說明,這個會在插入/查詢資料的時候是區分大小寫。
下面插入資料做些驗證:
1 mysql> insert lowupper (name, age, home) values ("shihuc", 30, "Beijing, china"); 2 Query OK, 1 row affected (0.00 sec) 3 4 mysql> 5 mysql> insert lowupper (name, age, home) values ("water", 33, "BEIJING, china"); 6 Query OK, 1 row affected (0.00 sec) 7 8 mysql> insert lowupper (name, age, home) values ("xiaocheng", 33, "hubei"); 9 Query OK, 1 row affected (0.00 sec)10 11 mysql> insert lowupper (name, age, home) values ("zhangsan", null, "china");12 Query OK, 1 row affected (0.00 sec)13 14 mysql> insert lowupper (name, age, home) values ("lisi", null, "CHINA");15 Query OK, 1 row affected (0.00 sec)16 17 mysql> insert lowupper (name, age, home) values ("wangwu", null, "China");18 Query OK, 1 row affected (0.00 sec)19 20 mysql> select * from lowupper;21 +-----------+------+----------------+22 | name | age | home |23 +-----------+------+----------------+24 | shihuc | 30 | Beijing, china |25 | water | 33 | BEIJING, china |26 | xiaocheng | 33 | hubei |27 | zhangsan | NULL | china |28 | lisi | NULL | CHINA |29 | wangwu | NULL | China |30 +-----------+------+----------------+31 6 rows in set (0.00 sec)
下面,再查詢一下看看,是否有區分:
1 mysql> select * from lowupper where home = "china";2 +----------+------+-------+3 | name | age | home |4 +----------+------+-------+5 | zhangsan | NULL | china |6 +----------+------+-------+7 1 row in set (0.00 sec)
再操作上面checknull表,在其中插入一條新的資料,進行查詢,看是否區分大小寫:
1 mysql> insert checknull (name, age) values ("SHIHUC", null); 2 Query OK, 1 row affected (0.00 sec) 3 4 mysql> select * from checknull; 5 +--------+------+ 6 | name | age | 7 +--------+------+ 8 | water | 30 | 9 | shihuc | NULL |10 | SHIHUC | NULL |11 +--------+------+12 3 rows in set (0.00 sec)13 14 mysql> select * from checknull where name = "shihuc";15 +--------+------+16 | name | age |17 +--------+------+18 | shihuc | NULL |19 | SHIHUC | NULL |20 +--------+------+21 2 rows in set (0.00 sec)22 23 mysql> select * from checknull where binary name = "shihuc";24 +--------+------+25 | name | age |26 +--------+------+27 | shihuc | NULL |28 +--------+------+29 1 row in set (0.00 sec)
是不是很顯然的,說明MySQL的大小寫問題,還是很有意思的,需要注意,在linux環境下。windows環境下,沒有測試,不是很確定。有經驗的可以分享一下!
http://www.cnblogs.com/shihuc/p/5165169.html
mysql中的null欄位值的處理及大小寫問題