0
1つのSQLクエリで各ブログ投稿のタグを取得しようとしています。私はposts
テーブル、post_tags
テーブル、tags
テーブルを持っています。投稿とタグの多対多関係を削除するには、post_tags
テーブルが存在します。SQL結合を使用して投稿タグを取得する
| p_id | c_id | u_id | title | body | published |
----------------------------------------------------------------------
| 1 | 1 | 1 | first post| lorem ipsum | 2012-01-27 18:37:47 |
| p_id | t_id |
---------------
| 1 | 3 |
| t_id | name | slug |
------------------------------------
| 3 | programming | programming |
そして、私が使用しているPHP方法:ここでは、それらのテーブルのスキーマのITはちょっと作品
public function getLatestPosts()
{
$query = $this->db->query('SELECT title, clean_title, body, published, name, slug
FROM posts
LEFT JOIN post_tags ON posts.p_id=post_tags.p_id
LEFT JOIN tags ON post_tags.t_id=tags.t_id
ORDER BY published DESC');
$posts = array();
foreach ($query->result() as $row)
{
$posts[] = array('title' => $row->title,
'clean_title' => $row->clean_title,
'body' => $row->body,
'published' => $row->published,
'tags' => array('name' => $row->name,
'slug' => $row->slug));
}
print_r($posts);
return $posts;
}
、ポストのタグは、各ポストのために検索されているが、私は重複した投稿があり。ここでは、クエリの結果のprint_r
ダンプです:
Array
(
[0] => Array
(
[title] => Second blog post, this is a pretty long title
[clean_title] => second-blog-post-this-is-a-pretty-long-title
[body] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eget est sit amet libero volutpat eleifend facilisis sollicitudin nisl. Nulla elit eros, semper eget tincidunt quis, egestas a nisl. Duis tempor tempus sapien consequat pellentesque. Cras vitae orci risus. Duis urna velit, interdum ac consectetur nec, condimentum non tellus. Sed nec nibh nisi. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras purus nulla, tincidunt quis placerat a, interdum at turpis. Pellentesque in lectus lacus. Sed at venenatis neque.
[published] => 2012-01-27 20:15:52
[tags] => Array
(
[name] =>
[slug] =>
)
)
[1] => Array
(
[title] => This is my first post!
[clean_title] => this-is-my-first-post
[body] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eget est sit amet libero volutpat eleifend facilisis sollicitudin nisl. Nulla elit eros, semper eget tincidunt quis, egestas a nisl. Duis tempor tempus sapien consequat pellentesque. Cras vitae orci risus. Duis urna velit, interdum ac consectetur nec, condimentum non tellus. Sed nec nibh nisi. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras purus nulla, tincidunt quis placerat a, interdum at turpis. Pellentesque in lectus lacus. Sed at venenatis neque.
[published] => 2012-01-27 18:37:47
[tags] => Array
(
[name] => programming
[slug] => programming
)
)
[2] => Array
(
[title] => This is my first post!
[clean_title] => this-is-my-first-post
[body] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eget est sit amet libero volutpat eleifend facilisis sollicitudin nisl. Nulla elit eros, semper eget tincidunt quis, egestas a nisl. Duis tempor tempus sapien consequat pellentesque. Cras vitae orci risus. Duis urna velit, interdum ac consectetur nec, condimentum non tellus. Sed nec nibh nisi. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras purus nulla, tincidunt quis placerat a, interdum at turpis. Pellentesque in lectus lacus. Sed at venenatis neque.
[published] => 2012-01-27 18:37:47
[tags] => Array
(
[name] => android
[slug] => android
)
)
[3] => Array
(
[title] => This is my first post!
[clean_title] => this-is-my-first-post
[body] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eget est sit amet libero volutpat eleifend facilisis sollicitudin nisl. Nulla elit eros, semper eget tincidunt quis, egestas a nisl. Duis tempor tempus sapien consequat pellentesque. Cras vitae orci risus. Duis urna velit, interdum ac consectetur nec, condimentum non tellus. Sed nec nibh nisi. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras purus nulla, tincidunt quis placerat a, interdum at turpis. Pellentesque in lectus lacus. Sed at venenatis neque.
[published] => 2012-01-27 18:37:47
[tags] => Array
(
[name] => windows
[slug] => windows
)
)
)
私はそれを見るように、代わりにポストのために各タグを返すので、それだけで別のタグでそのポストを複数返すています。私はそれが各投稿のtags
配列がその投稿の各タグの要素を持つようにそれが必要です。
すべてのヘルプは非常に高く評価される:)
重複質問:http://stackoverflow.com/質問/ 9047168/post-id-based-post-id/9047351#9047351 あなたの希望を得るために私の答えをチェックしてください。回答済みのソリューションで重複した行が発生すると述べました。 –