0

我想有一个图片库,每个画廊都将标有一组标签,我希望用户能够搜索任意数量的这些标签,我希望它有点效率。我真的不知道从哪里开始。

每个类别在“类别”表中都有一个条目,如下所示:

id | name | description
---+------+------------------------------------
0  | cats | This is a gallery full of cats
1  | dogs | This is a gallery full of dogs

并且“画廊”表中的每个条目都会为它们所属的每个类别存储多个类别 ID(尽管我不确定此时如何存储或查询它们)

如果不是我计划的搜索功能,我只会序列化类别数组,但这根本没有效率查询。

4

1 回答 1

1

求第三范式

 gallery 
 id | desc
----+---------
1   | dogs
2   | cats
3   | cats and dogs playing 

.

gallery_category
gallery_id | category_id 
-----------+-------------
 1         | 1
 2         | 0
 3         | 0
 3         | 1

然后使用联合相交

 select gallery_id from gallery_category where category_id=1
 INTERSECT 
 select gallery_id from gallery_category where category_id=0

在 gallery_category 的两列都有索引,这应该相当快。

不幸的是 mysql 不做相交,但你可以通过 join 获得相同的效果。

select a.gallery_id 
  from  gallery_category as a 
  join  gallery_category as b on a.gallery_id = b.gallery_id
  where a.category_id=1 and b.category_id=0
于 2012-05-17T00:43:12.410 回答