LoginSignup
1
2

More than 5 years have passed since last update.

SQLでcsv形式のカラムを利用して検索(SELECT)かけたいとき

Last updated at Posted at 2016-12-12

テーブルにCSV形式のカラムを作ってしまって、検索どうしよう...ってなったのでどうしたかをメモ。

正規表現は遅くなるかな、と思って LIKE でやる方法を見つけた。

select * from table where concat(',', column, ',') like '%,1,%'

先頭と末尾にカンマをくっつけてから %,3,% みたいな感じでLIKEすると検索できるっぽい(ググって見つけました)。

これでも相当無理やり感あるので、そもそも検索対象のカラムなのにCSV形式いしてしまうのが間違いかな。

と、思ったらMySQLには便利な関数があるそうで、以下の関数を使えば解決です。

select * from table where find_in_set('1',column);

パフォーマンスに関しても今後調査してみようと思います。

1
2
2

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