我有表是这样的:如何规范标签表
post:
+----------------+-----------+
| article_id | tags |
+----------------+-----------+
| 1 | x1,x2,x3|
| 2 | x1,x4,x5 |
+----------------+-----------+
我所试图做的是规范,所以我创建2个表:
tags:
+----------------+-----------+
| tag_id | tag_name |
+----------------+-----------+
| 1 | x1 |
| 2 | x2 |
| 3 | x3 |
+----------------+-----------+
post_tag:
+----------------+-----------+
| id_post | id_tag |
+----------------+-----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
+----------------+-----------+
我已经出口的所有标签从邮政表到标签表,现在每个标签都有自己的标签表中的标识,但我很困惑,当我想如何用post_tag表做到这一点?
编辑:questin是如何填充post_tag表,查询如何看起来像?
试过这样
$sql=mysql_query("SELECT * FROM post");
while($row=mysql_fetch_array($sql)) {
$article_id=$row['article_id'];
$all_tags =$row['tags'];
$tags= explode(",",$all_tags);
foreach($all_tags as $tag) {
$sql2=mysql_query("SELECT * FROM tags WHERE tag_name = '$tag'");
while($row=mysql_fetch_array($sql2)) { $tag_id=$row['tag_id']; $q = "INSERT INTO post_tag (id_post, id_tag) VALUE ('$article_id', $tag_id')";
$r = mysql_query($q);
}
}
}
但它不会写出完整post_tag表
这似乎罚款,删除旧列..你是什么困惑? –
你有什么问题? post_tag表格正常 – Fender
你有什么是帖子和标签之间的* n到n *关系。您的post_tag表格条目将充当一个标签和一个帖子之间的链接。像这样,你会将一个帖子链接到n个标签,并将一个标签链接到n个帖子。 – Eregrith