标签(Tag)的数据库设计

Segmentfault在文章编辑界面即有用到标签,那么这个是如何实现的呢?segmentfault

经过最爱的stackoverflow,找到了一些方法,其中用到的最多的一种解决方案以下:spa

简单的表结构code

首先增长一个Tag表,用来存储你的标签图片

请输入图片描述

请输入图片描述

涉及到的查询伪语句table

  • 插入新的Tag
    这个这么简单就不写咯,(>^ω^<)
  • 插入新的关联
    首先找到Tag对应的id
    select id from tag_table where Tag like %Tag%
    而后加入一条新的关联记录
    insert into bind_table (Tag, ObjectId) values (tag_id, object_id)
  • 经过Tag查询对应的ObjectId,单标签
    select b.ObjectId from bind_table b, tag_table t where t.Tag like %Tag% and b.Tag = t.Tag
  • 经过Tags查询对应的ObjectId,多标签
    select b.ObjectId from bind_table b, tag_table t where (t.Tag like %Tag% or b.Tag = t.Tag or ...) and b.Tag = t.id group by b.ObjectId having count(b.ObjectId) = Tags.length

就先写这么多了,玩去咯class

相关文章
相关标签/搜索