Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 连接表和循环_Mysql_Codeigniter - Fatal编程技术网

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();