在posts表中显示类别和标签(一对多关系)

时间:2017-02-13 12:09:41

标签: php mysql

我正在开发CMS。对于帖子类别和标签,我查看了WordPress数据库,采用了它的风格并进行了一些更改。

我有3张桌子:

table 1: posts (post_id,post_title)
table 2: terms (term_id,term_title,term_type)
table 3: term_relationships (tr_id,tr_post_id,tr_term_ir)

在表格中:term_type是cat(表示它是一个类别)或tag(表示它是一个标记)。

我想显示管理帖子的列表,但我无法找出为每个帖子显示catstags的正确方法(例如管理面板中的WordPress帖子页面)。

这是我到目前为止的尝试:

$posts_query=$db->query("SELECT `post_id`,`post_title` FROM `posts` ORDER BY `post_id` DESC");
while($post=mysqli_fetch_assoc($posts_query)){
     echo '<td>'.$post['post_title'].'</td>';
     $cats_query=$db->query("SELECT tr_term_id FROM term_relationships WHERE tr_post_id='{$post['post_id']}'") or die(mysqli_error($db->con));
     $term_ids='';
     while($cat=mysqli_fetch_assoc($cats_query)){
          $term_ids .=$cat['tr_term_id'].','; 
     }
     $term_ids= trim($term_ids,',');
     $cats=$db->query("SELECT `term_id`,`term_title` FROM `terms` WHERE `term_id` IN($term_ids) AND `term_type`='cat'");
          echo '<td>';
     while($cat= mysqli_fetch_assoc($cats)){
          echo $cat['term_title'].', ';
     }
     echo '</td>';
     }

它完成了这项工作,但却变得如此沉重和难以理解。我不认为这是正确的方法(我应该再添加一次并查询标签!)。

有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

您可以在一个查询中执行所有选择..

 SELECT `posts`.`post_id`, `posts`.`post_title` , `term_relationships`.`tr_term_id`, `terms`.`term_title`, 
 FROM `posts` as
 INNER JOIN `term_relationships` on `term_relationships`.`tr_post_id` = `posts`.`post_id`
 INNER JOIN `terms` on  `terms`.`term_id` = `term_relationships`.`tr_term_id`
 ORDER BY `posts`.`post_id` DESC

答案 1 :(得分:1)

试试这个:

      $posts_query=$db->query("SELECT p.`post_id`,p.`post_title`,GROUP_CONCAT((case when t.term_type='tag' then t.`term_title` end) SEPARATOR ',') as tags, GROUP_CONCAT((case when t.term_type='cat' then t.`term_title` end) SEPARATOR ',') as cats,
        GROUP_CONCAT((case when t.term_type not in('cat','tag') then t.`term_title` end) SEPARATOR ',') as others FROM `posts` p
        INNER JOIN  term_relationships  tr ON tr.tr_post_id=p.`post_id`
        INNER JOIN `terms` t ON  t.`term_id`=tr.tr_term_id
        GROUP BY p.`post_id` 
        ORDER BY p.`post_id` DESC");
        while($post=mysqli_fetch_assoc($posts_query)){
             echo '<td>'.$post['post_title'].'</td>';
             /*$cats_query=$db->query("SELECT tr_term_id FROM term_relationships WHERE tr_post_id='{$post['post_id']}'") or die(mysqli_error($db->con));
             $term_ids='';
             while($cat=mysqli_fetch_assoc($cats_query)){
                  $term_ids .=$cat['tr_term_id'].','; 
             }
             $term_ids= trim($term_ids,',');*/
             /*$term_ids= $post['term_ids'];
             $cats=$db->query("SELECT `term_id`,`term_title` FROM `terms` WHERE `term_id` IN($term_ids) AND `term_type`='cat'");
                  echo '<td>';
             while($cat= mysqli_fetch_assoc($cats)){
                  echo $cat['term_title'].', ';
             }*/
             echo '<td>';
             echo $post['tags'];
             echo '</td>';
             echo '<td>';
             echo $post['cats'];
             echo '</td>';
             echo '<td>';
             echo $post['others'];
             echo '</td>';
             }