LoginSignup
5
8

More than 5 years have passed since last update.

MySQLのNULLと空値でハマる

Posted at

たとえば

  • MySQL 5.6
  • hoge varchar(2) NULL可
  • 100件のレコードのうち、10件がNULL、90件が"AA"

ハマる

  • select count(1) from tablename where hoge is null : 10件 → わかる
  • select count(1) from tablename where hoge is not null : 90件 → わかる
  • select count(1) from tablename where hoge = 'AA' : 90件 → わかる
  • select count(1) from tablename where hoge = '' : 0件 → わかる
  • select count(1) from tablename where hoge != '' : 90件 → !!!!

言い訳

''自体がクソコードなわけですが、= ''!= ''が「逆」を意味しないのはいろいろと怖い

5
8
2

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
5
8