2
2

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】カンマ区切りで登録されているキーワードに対して前方一致検索を行う

Last updated at Posted at 2018-03-02

find_in_set()かつLIKE検索みたいなことをしたい

MySQLの**find_in_set()**ってLIKE検索みたいなこともできると思ってたんですけどできないみたいで困った。

↓みたいなことやりたかった

find_in_set('word%', keyword)

要は=(イコール)のものしか取れないんですよね。

仕方ないからLIKE文でがんばる

以下のようなカラム「keyword」にカンマ区切りで複数のキーワードを登録してるというテーブル「hoge」があるという想定です。

mysql> select * from hoge;
+----+--------------------+-----------------------------------------------+---------------------+---------------------+
| id |  name              | keyword                                       | created_at          | updated_at          |
+----+--------------------+-----------------------------------------------+---------------------+---------------------+
|  1 |  キーワード1        | キーワード1,test,keyword1                      | 2018-02-22 09:48:12 | 2018-02-22 09:48:12 |
|  2 |  キーワード2        | test2,キーワード2,keyword2                     | 2018-02-22 09:48:12 | 2018-02-22 09:48:12 |
|  3 |  キーワード3        | keyword3,キーワード3,aaa                       | 2018-02-22 09:48:12 | 2018-02-22 09:48:12 |
|  4 |  キーワード4        | キーワード4,iiiii,ooooo                        | 2018-02-22 09:48:12 | 2018-02-22 09:48:12 |
|  5 |  キーワード5        | きいわあど5,キーワード5,testarossa             | 2018-02-22 09:48:12 | 2018-02-22 09:48:12 |
+----+--------------------+-----------------------------------------------+----------------------+--------------------+
5 rows in set (0.00 sec)

カラム「keyword」にはどんな順番でキーワードが登録されるのかわからないので先頭 or 2番目以降のカンマ区切りのキーワードに対して前方一致するように以下のように書きました。

mysql> select * from hoge where keyword like 'te%' or keyword like '%,te%' limit 10;
+----+--------------------+-----------------------------------------------+---------------------+---------------------+
| id |  name              | keyword                                       | created_at          | updated_at          |
+----+--------------------+-----------------------------------------------+---------------------+---------------------+
|  1 |  キーワード1        | キーワード1,test,keyword1                      | 2018-02-22 09:48:12 | 2018-02-22 09:48:12 |
|  2 |  キーワード2        | test2,キーワード2,keyword2                     | 2018-02-22 09:48:12 | 2018-02-22 09:48:12 |
|  5 |  キーワード5        | きいわあど5,キーワード5,testarossa             | 2018-02-22 09:48:12 | 2018-02-22 09:48:12 |
+----+--------------------+-----------------------------------------------+----------------------+--------------------+

無事取得することができた。

おわり

  • keywordが大量にあったりデータが多かったらめちゃくちゃ時間かかりそうです・・・

参考

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?