PHP - 制作规范化关键字表
我有一个带有 article (article_id,关键字)数据库表的 mysql 数据库,其中关键字是逗号分隔的列表。我创建了两个新的空表:
keywords(keyword_id,keyword),其中每一行只有一个关键字。
article_key (article_id, keywords_id),其中每一行只有一个关键字。
因此,我希望从文章到关键字建立多对多的关系。我想创建一个简短的脚本来从原始 article 表中提取 id 和关键字,拆分关键字并适当地填充两个新表。我以为这很容易,但事实证明比我想象的要棘手一些。
任何帮助将不胜感激。
I have a mysql db with an article (article_id, keyword) db table where keywords is a comma-separated list. I have created two new empty tables:
keywords (keyword_id, keyword) where each row has only one keyword.
article_key (article_id, keyword_id) where each row has only one keyword.
Thus I want to have a many-to-many relationship from articles to keywords. I wanted to create a short script to extract the id and keywords from the original article table, split the keywords and populate the two new tables appropriately. I assumed this would be easy, but has proved a little more tricky than I thought.
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行与此类似的操作:
在此脚本中(未经测试),我假设您的 keywords 表已填充关键字,并且您的 article 表包含逗号KEYWORDS 和 NOT id 的单独列表。
You could do something similar to this:
In this script (not tested) I assumed you already have your keywords table filled with the keywords, and that your article table contains a comma seperated list of KEYWORDS and NOT id's.
不太确定您的问题是什么,我假设在
article_key
中您的意思是keyword_id
而不是keyword
要获取文章的关键字(给定的article_id):
要获取与关键字关联的文章(给定的keyword_id):
编辑
错过了您的实际问题,只是看到了它。下面 Marc B 的回答很好。
Not quite sure what your question is, and I'm assuming that in
article_key
you meantkeyword_id
rather thankeyword
To get the keywords of an article (given article_id):
To get the articles that a keyword is associated with (given keyword_id):
Edit
Missed your actual question, just saw it though. Marc B's answer below is a good one.
在伪代码中:
In pseudo-code: