16
11

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 5 years have passed since last update.

【MySQL】existsの挙動まとめ

Last updated at Posted at 2017-08-13

まとめようとしたきっかけ

不具合を直そうと思ってexistsを使った結果、使い方を誤解していたために、別の新たな不具合を仕込みかけた:sweat_drops:
「select *」ではなく、存在チェックをしたかったカラムをピンポイントで取得しようとするときに(select nameなど)にハマった罠でした。

サンプルデータ

テーブル名:member

id name sex div age
1 田中 営業 25
2 佐藤 34
3 池田 開発 28

select * の場合

特に問題ない。引っかかるポイントはないはず。

where sex = "男"

取得結果は・・・↓

id name sex div age
1 田中 営業 25
2 佐藤 34
レコードが取得できたので、この時のexistsの結果は「1(true)」。
select exists (select * from member where sex = '男');

where div = "総務"

取得結果は・・・↓

id name sex div age
div = '総務'というレコードは存在しない=レコードが取得できないので、existsの結果は「0(false)」。
select exists (select * from member where div = '総務');

select divの場合

1カラムだけ取ってくるので、カラムの値とレコードの有無がごっちゃになってしまいがち:droplet:

where name = "田中"

取得結果は・・・↓

div
営業
レコードが取得できているので、existsの結果は「1(true)」。
select exists (select div from member where name = '田中');

where name = "佐藤"

取得結果は・・・↓

div
レコードが取得できているので、existsの結果は「1(true)」
select exists (select div from member where name = '佐藤');

ひっかかりポイント!

このselect文では1レコード取得できている。

select * from member where name = '佐藤';

あくまでも、divというカラムがnullとか空文字だっただけで、レコードとしては存在している。
そのため、existsは「1(true)」になる。

nullとか空文字であることを判定したいなら、ifnullとかがよい。

16
11
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
16
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?