10
8

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.

【CakePHP】SQLでcollate utf8_unicode_cを使う

Last updated at Posted at 2015-06-02

MySQLで照合順序がutf8_general_ciのカラムに対して、utf8_unicode_ci のあいまい検索を行う場合は以下のようにする。

select * from member where namae collate utf8_unicode_ci like '%hoge%';

Cakeで上記を実行した場合そのままではうまくいかなかったためメモ。

以下だとCakeで発行されるSQLのname collate utf8_unicode_ciにバッククオートがつくためエラーとなる。

失敗例
$this->find('all',[
  'conditions'=>['name collate utf8_unicode_ci like'=>'%hoge%'
]);

正しくはこれ。
カッコでくくるか、バッククオートをつけることでカラムにのみバッククオートがつくようになる。

成功例
$this->find('all',[
  'conditions'=>['(name) collate utf8_unicode_ci like'=>'%hoge%'
]);
成功例
$this->find('all',[
  'conditions'=>['`name` collate utf8_unicode_ci like'=>'%hoge%'
]);
10
8
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
10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?