php - mysql通过不返回左表中的所有结果来左连接和分组

标签 php mysql join activerecord group-concat

我有以下表格:

额外问题:

enter image description here

和extra_examples:

enter image description here

我想加入并连接它们,以便返回所有问题,但在每个问题中,我都会显示一个下拉列表,其中包含来自 extra_examples 的所有问题标签。

我目前有这个声明(使用 codeigniter/activerecord):

$this->db->select('extra_questions.*, GROUP_CONCAT(extra_examples.tag) as tag',FALSE);
        $this->db->from('extra_questions');
        $this->db->join('extra_examples','extra_questions.id = extra_examples.question','left outer');
        $this->db->group_by('extra_examples.question');
        $this->db->order_by('extra_questions.id');
        $query = $this->db->get();
        return $query->result_array();

作为 $questions 返回。但是当我遍历 $questions 时:

 <?php foreach ($questions as $i=>$questions_item): ?>
            <?php print_r($questions_item['id']); print_r($questions_item['tag']); echo '<br/><br/>' ?>
<?php endforeach ?>

我得到了一些奇怪的结果:

2flower,insect

3

我期望的地方:

2花虫 3个 4

但是 4 完全不见了。我想确保返回所有问题。我做错了什么?

最佳答案

你的分组依据应该在 extra_questions.id 而不是 extra examples.question 上

关于php - mysql通过不返回左表中的所有结果来左连接和分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32628321/

相关文章:

php - 访问由 while 循环生成的特殊 div

php - 可以从 php 中的数组动态定义类默认公共(public)变量吗?

mysql - Codeigniter 中的 session (显示活跃用户)

mysql - MySQL 中何时使用单引号、双引号和反引号

mysql - 连接 2 个或更多数据库与 1 列结果

php - 连接查询中的列以使用 'LIKE' 运算符

php - 在 PHP 中合并类成员

mysql - 如何在 INSERT INTO table VALUES (....) 期间检查某个单词是否在一组允许的单词中?

mysql - 从子表中选择计数

sql - 在 firebird select 过程中显示给定月份和年份的所有日期