搜索具有以逗号分隔的多个值的列的最佳方法是什么

时间:2014-10-23 17:49:00

标签: mysql sql

我有一个名为Genre的列,其值为Action, Crime, Mystery, Comedy

我需要在列中搜索Crime,Mystery例如

我怎样才能有效地做到这一点

例如sudo代码select * from movie where genre is thriller and romanceselect * from movie where genre is action and comedy

为了澄清,在第二个查询中我需要回归的电影在他们的类型字段中有动作和喜剧

2 个答案:

答案 0 :(得分:1)

您可以使用find_in_set

select * from movie
where find_in_set('Crime',genre) > 0
and find_in_set('Romance',genre) > 0

答案 1 :(得分:0)

您基本上可以正确使用它,但您可能必须在查询中使用wild cards。这些可以放在你想要的字符串部分之前和之后。

Select * FROM movie WHERE genre like '%Crime%' or genre like '%Action%'

但老实说,你应该以不同的方式组织你的数据。