MYSQLの罠

テーブルを眺めてたら懐かしい罠を発見したのでメモ

こんなデータが入ったテーブルがあります。

mysql> select * from SEARCH_TEST;

                                          • +
ID DESC1 DESC2
                                          • +
1 test test
2 TEST TEST
                                          • +

2 rows in set (0.00 sec)

同じデータですが検索結果が異なります。
さてなぜでしょう

mysql> select * from SEARCH_TEST where DESC1 like 'test';

                                          • +
ID DESC1 DESC2
                                          • +
1 test test
                                          • +

1 row in set (0.00 sec)
mysql> select * from SEARCH_TEST where DESC2 like 'test';

                                          • +
ID DESC1 DESC2
                                          • +
1 test test
2 TEST TEST
                                          • +

2 rows in set (0.00 sec)

正解は「ujis_bin」指定をしている事でした。
この指定をすると大文字小文字を区別します。
ちなみにtext型は大文字小文字を区別をしないです。
検索処理を作る人は気をつけときましょう。

`DESC1` varchar(64) character set ujis collate ujis_bin default NULL,
`DESC2` varchar(64) default NULL,