wondering how i can create a tagging system in php and with a mysql database, my initial thoughts were to create a row in the table where the articles are stored, named tags, and list the tags seperated by commas, but i am not sure how i can create a query that searches for matching tags, i dont want to have to query every article everytime someone clicks a tag. can anyone help?
我想知道如何在php和mysql数据库中创建一个标记系统,我最初的想法是在表格中创建一个存储文章的行,命名标签,并列出用逗号分隔的标签,但我不知道如何我可以创建一个搜索匹配标签的查询,我不想每次有人点击标签时查询每篇文章。有人可以帮忙吗?
2 个解决方案
#1
30
You're describing a many-to-many relationship between Articles and Tags. You'd want to use an intermediate junction table to resolve that relationship.
您正在描述文章和标签之间的多对多关系。您希望使用中间联结表来解决该关系。
Now, to find all articles that match a selected tag:
现在,要查找与所选标记匹配的所有文章:
SELECT a.article_title
FROM Tag t
INNER JOIN Article_Tag_Xref atx
ON t.tag_id = atx.tag_id
INNER JOIN Article a
ON atx.article_id = a.article_id
WHERE t.tag_name = 'SelectedTagName'
#2
2
tbl_articles has articleID, etc.
tbl_articles有articleID等。
tbl_tags has tagID, etc.
tbl_tags有tagID等。
tbl_taggedarticles has taggedArticleID, articleID, tagID
tbl_taggedarticles有taggedArticleID,articleID,tagID
SELECT articleID FROM tbl_taggedarticles WHERE tagID = "searchedID"
#1
30
You're describing a many-to-many relationship between Articles and Tags. You'd want to use an intermediate junction table to resolve that relationship.
您正在描述文章和标签之间的多对多关系。您希望使用中间联结表来解决该关系。
Now, to find all articles that match a selected tag:
现在,要查找与所选标记匹配的所有文章:
SELECT a.article_title
FROM Tag t
INNER JOIN Article_Tag_Xref atx
ON t.tag_id = atx.tag_id
INNER JOIN Article a
ON atx.article_id = a.article_id
WHERE t.tag_name = 'SelectedTagName'
#2
2
tbl_articles has articleID, etc.
tbl_articles有articleID等。
tbl_tags has tagID, etc.
tbl_tags有tagID等。
tbl_taggedarticles has taggedArticleID, articleID, tagID
tbl_taggedarticles有taggedArticleID,articleID,tagID
SELECT articleID FROM tbl_taggedarticles WHERE tagID = "searchedID"