Mysql 连接表和循环
我已将帖子和标签的表格合并为以下内容:Mysql 连接表和循环,mysql,codeigniter,Mysql,Codeigniter,我已将帖子和标签的表格合并为以下内容: $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'); 如何循环浏览每个帖子
$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
目前,我可以显示标签,但它返回了两行post 1和post 2。现在的输出是:
post1: tag1
post1: tag2
post2: tag1
post2: tag3
如何为post返回一行,并在其中包含所有相关标记?使用group concat和group results
$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();