产品页面上的连锁类别列表

时间:2014-02-06 17:09:19

标签: php mysql

我的数据库列为

id  |  item  |  category
=========================
1   |  Pen   |  featured
2   | Pencil |  featured,new arrival
3   | eraser |  new arrival

在我的列表页面上,我将根据类别列出这些产品,即查询WHERE子句取决于类别名称(特色,新到货)。

请告诉我如何实现这一目标。

2 个答案:

答案 0 :(得分:1)

您可以使用find_in_set(str, strlist)功能。

  

如果字符串str在包含N个子串的字符串列表strlist中,则返回1到N范围内的值。

select * from items 
 where find_in_set( 'new arrival', category ) > 0

请参阅

答案 1 :(得分:0)

如果您的表格允许重复的项目,您可以将其更改为例如。

id  |  item  |  category
=========================
1   |  Pen   |  featured
2   | Pencil |  featured
3   | Pencil |  new arrival
4   | eraser |  new arrival

你可以查询不同的项目。 e.g。

select distinct item from table where category in ('featured','new arrival');
相关问题