国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL NULL 值處理實例詳解

2024-07-24 12:52:54
字體:
來源:轉載
供稿:網友

MySQL NULL 值處理

我們已經知道MySQL使用 SQL SELECT 命令及 WHERE 子句來讀取數據表中的數據,但是當提供的查詢條件字段為 NULL 時,該命令可能就無法正常工作。

為了處理這種情況,MySQL提供了三大運算符:

IS NULL: 當列的值是NULL,此運算符返回true。 IS NOT NULL: 當列的值不為NULL, 運算符返回true。 <=>: 比較操作符(不同于=運算符),當比較的的兩個值為NULL時返回true。

關于 NULL 的條件比較運算是比較特殊的。你不能使用 = NULL 或 != NULL 在列中查找 NULL 值 。

在MySQL中,NULL值與任何其它值的比較(即使是NULL)永遠返回false,即 NULL = NULL 返回false 。

MySQL中處理NULL使用IS NULL和IS NOT NULL運算符。

在命令提示符中使用 NULL 值

以下實例中假設數據庫 TUTORIALS 中的表 tcount_tbl 含有兩列 tutorial_author 和 tutorial_count, tutorial_count 中設置插入NULL值。

實例

嘗試以下實例:

root@host# mysql -u root -p password;Enter password:*******mysql> use TUTORIALS;Database changedmysql> create table tcount_tbl -> ( -> tutorial_author varchar(40) NOT NULL, -> tutorial_count INT -> );Query OK, 0 rows affected (0.05 sec)mysql> INSERT INTO tcount_tbl -> (tutorial_author, tutorial_count) values ('mahran', 20);mysql> INSERT INTO tcount_tbl -> (tutorial_author, tutorial_count) values ('mahnaz', NULL);mysql> INSERT INTO tcount_tbl -> (tutorial_author, tutorial_count) values ('Jen', NULL);mysql> INSERT INTO tcount_tbl -> (tutorial_author, tutorial_count) values ('Gill', 20);mysql> SELECT * from tcount_tbl;+-----------------+----------------+| tutorial_author | tutorial_count |+-----------------+----------------+| mahran | 20 || mahnaz | NULL || Jen | NULL || Gill | 20 |+-----------------+----------------+4 rows in set (0.00 sec)mysql>

以下實例中你可以看到 = 和 != 運算符是不起作用的:

mysql> SELECT * FROM tcount_tbl WHERE tutorial_count = NULL;Empty set (0.00 sec)mysql> SELECT * FROM tcount_tbl WHERE tutorial_count != NULL;Empty set (0.01 sec)

查找數據表中 tutorial_count 列是否為 NULL,必須使用IS NULL和IS NOT NULL,如下實例:

mysql> SELECT * FROM tcount_tbl -> WHERE tutorial_count IS NULL;+-----------------+----------------+| tutorial_author | tutorial_count |+-----------------+----------------+| mahnaz | NULL || Jen | NULL |+-----------------+----------------+2 rows in set (0.00 sec)mysql> SELECT * from tcount_tbl -> WHERE tutorial_count IS NOT NULL;+-----------------+----------------+| tutorial_author | tutorial_count |+-----------------+----------------+| mahran | 20 || Gill | 20 |+-----------------+----------------+2 rows in set (0.00 sec)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黑山县| 邛崃市| 孟津县| 榆社县| 老河口市| 宁德市| 馆陶县| 喜德县| 文安县| 九龙县| 察隅县| 阿拉善左旗| 龙海市| 城固县| 灌阳县| 保康县| 湘西| 湄潭县| 石景山区| 手机| 松滋市| 两当县| 卢湾区| 玛曲县| 页游| 绥中县| 昌乐县| 清新县| 邯郸县| 牙克石市| 寿光市| 东城区| 洛扎县| 新龙县| 平湖市| 比如县| 农安县| 海兴县| 沈丘县| 类乌齐县| 渝北区|