2012-09-07 73 views
0

我有表是这样的:如何规范标签表

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表

+2

这似乎罚款,删除旧列..你是什么困惑? –

+0

你有什么问题? post_tag表格正常 – Fender

+0

你有什么是帖子和标签之间的* n到n *关系。您的post_tag表格条目将充当一个标签和一个帖子之间的链接。像这样,你会将一个帖子链接到n个标签,并将一个标签链接到n个帖子。 – Eregrith

回答

1

也许这一个将工作?

$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_title) { 
     $tag_id = null; 
     $tag=mysql_fetch_assoc(mysql_query('SELECT * FROM tags WHERE tag_name = "'.$tag_title.'"')); 
     if ($tag) { 
      $tag_id = $tag['tag_id']; 
     } else { 
      mysql_query('INSERT INTO tags SET tag_name = "'.$tag_title.'"'); 
      $tag_id = mysql_insert_id(); 
     } 
     $q = "INSERT INTO post_tag (id_post, id_tag) VALUE ('$article_id', $tag_id')"; 
     $r = mysql_query($q); 
    } 
} 

运行循环后,您将需要post

+0

非常感谢你(需要等待14小时的赏金声望) – InTry

+0

没问题,让我知道如果我可以帮助任何事情,欢呼声 –

1

对于每一个标签的帖子有,您将在post_tag表中的条目。这被称为m-to-n关系。谷歌它;)

  • POST_ID:1,TAG_ID:1
  • POST_ID:1,TAG_ID:2
  • POST_ID:1,TAG_ID:3

  • post_id

标记

  • TAG_ID
  • TAG_NAME

post_tag

  • POST_ID
  • TAG_ID
0

假设你有一个叫my cat!后,它具有的一个id。您希望将标签catshome添加到它。

您应该首先在tags表中创建标记catshome(如果它们不存在)。可以说他们是cats | 2home | 4(数字是他们的id)。

然后,您需要在post_tag表中存储值1 | 21 | 4,其中第一个值是帖子的ID,第二个值是标签的ID。

要找到适合岗位数1标签,你想要的东西像

SELECT Tags.name FROM Tags, Tags_Posts WHERE Tags_Posts.post_id = 1 AND Tags.id = Tags_Posts.tag_id

+0

这一切都清楚,我明白了,但我很困惑如何与现有的数据库做到这一点,如何MySQL查询应该看起来像...如何填充post_tag表? – InTry

+0

编辑我的回答 – Ivo

+0

谢谢伊沃,但我想从现有的帖子,标签表填充post_tag表 – InTry

1

我明白了,你想用一个SQL查询来解决您的问题,但它不是方便。尝试使用php-power,你就会成功。

php的方式。 1.创建表格tagsposts_tags(就像你在问题中描述的那样)。 2.在PHP从当前post表和循环得到它的所有行:

foreach($query_results as $row) { 
    $article_id = $row['article_id]; 
    $tags = explode('|', $row['tags']); 
    foreach($tags as $tag) { 
    // here check if this tag exists in your new `tags` table (by tag's name). If it is, save its id in $tag_id variable. 
    // if a tag don't exists then insert it. And put its new id (mysql returns it after inserting) into $tag_id. 
    // After that add a row ($article_id, $tag_id) to table `posts_tags` 
    } 
} 

然后从旧post表中删除列tags。利润!

p.s.在表中以及在posts_tags表中对(article_id,tag_id)创建标签名称的唯一索引。

+0

你“描述”的部分正是让我困惑的部分,它是循环内循环,内部循环,... – InTry

+0

这里只有两个循环。没关系。请注意,在这种情况下,不需要优化,因为您只需执行一次该操作(重新创建数据库)即可。 –