如何根据多个值字段编写查询以从表过滤器(WHERE子句)中获取值?

时间:2014-01-02 11:01:41

标签: php mysql sql

我在MySQL中有一个产品表,字段如下所述,

id      productname     price   currency      description      category
-------------------------------------------------------------------------
34      xxxx            50      USD           xyxyxyxyxyx       54,65
35      xxyy            60      USD           xyxyxyxyxyx       52,75
36      yyxx            70      USD           xyxyxyxyxyx       24,85
37      yyyy            20      USD           xyxyxyxyxyx       65,52

我的情况是产品可以在不同的类别中使用。所以我用逗号分隔符存储多个类别,如上表所示。

现在我想检索特定类别的产品,例如,检索52类产品。

结果应该是这样的

id      productname     price   currency      description   category
-------------------------------------------------------------------------
35      xxyy            60      USD           xyxyxyxyxyx       52,75
37      yyyy            20      USD           xyxyxyxyxyx       65,52

如何编写查询以获得此结果?

提前非常感谢!。

1 个答案:

答案 0 :(得分:1)

尝试使用FIND_IN_SET(str,strlist)方法,

select * from tablename where FIND_IN_SET('52',category) > 0