「MySQL」値がNULLであるかを調べるサンプル

2022年1月5日

環境
Windows10 64bit
MySQL 8.0.27

書式
ISNULL( 値 )
ISNULL関数を使用して引数の値がNULLであるか調べます
ISNULL関数は、引数がNULLの場合は1(TRUE)を、引数がNULL出ない場合は0(FALSE)を返します。

使用例1
引数がNULLの場合

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> SELECT ISNULL(NULL) resA, ISNULL(21/0) resB;
+------+------+
| resA | resB |
+------+------+
| 1 | 1 |
+------+------+
1 row in set (0.00 sec)
mysql> SELECT ISNULL(NULL) resA, ISNULL(21/0) resB; +------+------+ | resA | resB | +------+------+ | 1 | 1 | +------+------+ 1 row in set (0.00 sec)
mysql>  SELECT ISNULL(NULL) resA, ISNULL(21/0) resB;
+------+------+
| resA | resB |
+------+------+
|    1 |    1 |
+------+------+
1 row in set (0.00 sec)

使用例2
引数がNULLではない場合

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> SELECT ISNULL('study') resA, ISNULL(789) resB;
+------+------+
| resA | resB |
+------+------+
| 0 | 0 |
+------+------+
1 row in set (0.00 sec)
mysql> SELECT ISNULL('study') resA, ISNULL(789) resB; +------+------+ | resA | resB | +------+------+ | 0 | 0 | +------+------+ 1 row in set (0.00 sec)
mysql>  SELECT ISNULL('study') resA, ISNULL(789) resB;
+------+------+
| resA | resB |
+------+------+
|    0 |    0 |
+------+------+
1 row in set (0.00 sec)

使用例3
ISNULL関数以外で確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> SELECT (NULL <=> NULL) resA, (NULL IS NULL) resB;
+------+------+
| resA | resB |
+------+------+
| 1 | 1 |
+------+------+
1 row in set (0.00 sec)
mysql> SELECT (NULL <=> NULL) resA, (NULL IS NULL) resB; +------+------+ | resA | resB | +------+------+ | 1 | 1 | +------+------+ 1 row in set (0.00 sec)
mysql> SELECT (NULL <=> NULL) resA, (NULL IS NULL) resB;
+------+------+
| resA | resB |
+------+------+
|    1 |    1 |
+------+------+
1 row in set (0.00 sec)

 

MySQL

Posted by arkgame