0

次のような投稿とタグのテーブルを結合しました。

$this->db->select('*');
$this->db->from('posts');
$this->db->join('posts_tags', 'posts.post_id = posts_tags.post_id', 'left');
$this->db->join('tags', 'posts_tags.tag_id = tags.tag_id', 'left');

各投稿をループして、それぞれのタグのリストを表示するにはどうすればよいですか。例えば:

post1: tag1, tag2
post2, tag1, tag3

現時点では、タグを表示できますが、投稿 1 と 2 で 2 つの行が返されます。出力は次のとおりです。

post1: tag1
post1: tag2
post2: tag1
post2: tag3

関連するすべてのタグを内部に含めて、投稿用に 1 行を返すにはどうすればよいですか?

4

1 に答える 1

1

グループ連結とグループ結果を使用する

$this->db->select('posts.*');
$this->db->select('GROUP_CONCAT(posts_tags.tag_title) as TagTitles');
$this->db->from('posts');
$this->db->join('posts_tags', 'posts.post_id = posts_tags.post_id', 'left');
$this->db->join('tags', 'posts_tags.tag_id = tags.tag_id', 'left');
$this->db->group_by('posts.id');
$this->db->get();
于 2013-01-26T18:39:09.053 に答える