MySQLのちょっとしたTIPS

MySQLってこんなの書けるんだなーって再認識。


mysql> set @1 = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> select 1 as k from dual where if( @1, 1, 0 );
Empty set (0.00 sec)

mysql> set @1 = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> select 1 as k from dual where if( @1, 1, 0 );

k
1

1 row in set (0.00 sec)

DUALテーブル使うことでSELECT節だけじゃなくてWHERE節で真偽値の判定が出来る。