私はこのようなテーブルを持っていました:
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 テーブルから tags テーブルにエクスポートし、各タグに独自の ID がタグ テーブルに含まれるようになりましたが、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 テーブルを書きません