57
44

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] SQLでif文を使う

Last updated at Posted at 2015-06-19

SQLでif文的なことをやる方法を記載

phpなどでif文を記述した場合


if(param1 == 0){
 return 'なし';
} else if(param1 == 1){
 return 'あり';
} else {
return null;
}

mysqlでcase文を記述した場合

CASE WHEN param1=0 THEN 'なし'
   WHEN param1=1 THEN 'あり'
   ELSE  null END

例:
param1の返り値が0または1の場合に「なし」、「あり」の文言に変換して返したい場合

select 
  (CASE WHEN param1=0 THEN 'なし'
   WHEN param1=1 THEN 'あり'
   ELSE  null END) as param1_display
from db_table;

以上

57
44
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
57
44

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?