且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在mysql的列中保存多个项目

更新时间:2023-09-05 13:41:04

不要将topic_id存储在paper表中.而是创建另一个将topic_id链接到paper_id的规范化(多对多)表.

Don't store topic_id in the paper table. Instead, create another normalized (many-to-many) table which links topic_id to paper_id.

/* Each paper can exist in this table as many times as necessary for all its topics */
CREATE TABLE paper_topics (
  paper_id INT NOT NULL,
  topic_id INT NOT NULL,
  FOREIGN KEY (paper_id) REFERENCES paper (paper_id),
  FOREIGN KEY (topic_id) REFERENCES topic (topic_id),
  PRIMARY KEY (paper_id, topic_id)
);

这将使您可以根据需要在每篇论文中存储尽可能多的主题.

This will allow you to store as many topics per paper as necessary.

要检索论文的主题,请使用:

To retrieve the topics for a paper, use:

SELECT 
  paper.*,
  topic_name
FROM 
  paper
  LEFT JOIN paper_topics ON paper.paper_id = topic.paper_id
  LEFT JOIN topic ON topic.topic_id = paper_topic.topic_id
WHERE paper.paper_id = <some paper id value>

尝试将多个值存储在一个列中(例如,在paper表中以逗号分隔的topic_id列表)几乎不是一个好主意.原因是为了对其进行查询,必须使用 FIND_IN_SET() ,这提高了执行连接的复杂性,并使得在查询时无法利用列索引.

It is just about never a good idea to attempt to store multiple values in one column (such as a comma-separated list of topic_id in the paper table). The reason is that in order to query against it, you must use FIND_IN_SET() which drives up the complexity of performing joins and makes it impossible to utilize a column index when querying.