这是一个经典的n到m的关系.像这样
tags table
----------
id
name
...
content table
-------------
id
title
...
content_tags table
------------------
content_id
tag_id
Run Code Online (Sandbox Code Playgroud)
要为单个内容存储多个标记,请在content_tags表中为您希望它拥有的每个标记添加记录.例:
content_tags table
------------------
content_id | tag_id
1 | 2
1 | 5
1 | 1
Run Code Online (Sandbox Code Playgroud)
然后获取内容记录的标记:
select t.*
from tags t
join content_tags ct on ct.tag_id = t.id
join content c on ct.content_id = c.id
where c.title = 'my content title'
Run Code Online (Sandbox Code Playgroud)