2017-02-13 4 views
3

私はCMSを開発中です。ポストカテゴリとタグについては、WordPressデータベースを見て、そのスタイルを採用し、いくつかの変更を加えました。表用語で投稿テーブルのカテゴリとタグを表示します(1対多の関係)

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) 

私は3つのテーブルを持っている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>'; 
    } 

それは仕事をしていませんが、それはとても重いとハードに理解を取得します。私はこれが正しい方法だとは思わない(タグを追加してクエリを追加する必要があります)。

これを行うより良い方法はありますか?

答えて

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>'; 
      } 
+0

それは地獄です:)非常に良い、ありがとう、あなたがGROUP(GROUP p.'post_id')の後にBYを追加するのを忘れたただ1つ私はそれを追加し、それは働いた。投稿タグと猫を別々に表示するにはどうすればいいですか?私はtd(タグ:term_type =タグ、cats:term_type = cat)のそれぞれのものを欲しい。 – theboy

+0

私のコメントを編集してください。 – theboy

+0

これを試してください:GROUP_CONCAT(t.term_type = 'タグ'、その後t.'term_title'終了)SEPARATOR '、')タグ、GROUP_CONCAT((t.term_type = 'cat'、t.'term_title'最後に)SEPARATOR '、')猫として –

1

あなたは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 
+0

感謝の男。もう1つの答えはより詳細なので、私はそれを試しました(私は本当にJOINに精通していません)。 – theboy

関連する問題