3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【SQL】NULLと空文字の扱い

Last updated at Posted at 2020-03-07

SQLの使い方を調べていて、NULLと空文字の扱いについて疑問が出たのでメモ。

##NULLと空文字
大前提NULLと空文字は別物で、
NULL:何もない
空文字:長さ0文字の文字列
ってことらしい。

で、SQLではNULLと空文字が厳密に判定される、とのこと。このサイトが分かりやすい。

##MySQLでのNULLと空文字の扱い

▼空文字のみヒット

SELECT * FROM tbl_name WHERE str = '';

▼NULLのみヒット

SELECT * FROM tbl_name WHERE str IS NULL;

▼ 空文字とNULL値が除外される(str != '';の挙動に注意)

SELECT * FROM tbl_name WHERE str != '';

▼ NULL値が除外される

SELECT * FROM tbl_name WHERE str IS NOT NULL;
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?