【问题标题】:I cant loop record from table which already use GROUP BY我不能从已经使用 GROUP BY 的表中循环记录
【发布时间】:2016-03-20 19:43:26
【问题描述】:

我想从表中循环记录字段。我在查询中使用 LEFT JOIN 并使用 GROUP BY 来防止结果出现双循环。查询成功但结果如下:

===========================================
|No|Nama Sekolah|Alamat|Telepon|Kompetensi|
===========================================
|1 |SMP 1       |JKT   |021231 | 1.TIK1   |
|2 |SMP 2       |BDG   |021232 | 1.RPL1   |
===========================================

如果我不使用 GROUP BY,结果将是这样的:

===========================================
|No|Nama Sekolah|Alamat|Telepon|Kompetensi|
===========================================
|1 |SMP 1       |JKT   |021231 | 1.TIK1   |
|2 |SMP 1       |JKT   |021231 | 1.TIK2   |
|3 |SMP 2       |BDG   |021232 | 1.RPL1   |
|4 |SMP 2       |BDG   |021232 | 1.RPL2   |
===========================================

我想要这样的结果:

===========================================
|No|Nama Sekolah|Alamat|Telepon|Kompetensi|
===========================================
|1 |SMP 1       |JKT   |021231 | 1.TIK1   |
|  |            |      |       | 2.TIK2   |
|2 |SMP 2       |BDG   |021232 | 1.RPL1   |
|  |            |      |       | 2.RPL2   |
===========================================

我正在使用 codeigniter 框架。

这是我的看法:

<table class='table table-bordered'>
    <thead>
        <tr>
            <td>No</td>
            <td>Lokasi</td>
            <td>Alamat</td>
            <td>Telepon</td>
            <td>Kompetensi</td>
        </tr>
    </thead>
        <?php
        $i = 0;
        foreach ($row as $row) {
            $i++;
            echo"<tr>
                    <td>".$i."</td>
                    <td>".$row->nama_sekolah."</td>
                    <td>".$row->alamat."</td>
                    <td>".$row->no_telp."</td>
                    <td><ol>
                            <li>".$row->nama_jurusan."</li>
                        </ol>
                    </td>
                </tr>";
        }
        ?>
</table>

这是模型:

function getlokasi($jenjang){
    $sql = "SELECT s.nama_sekolah, s.alamat, s.no_telp, j.nama_jurusan FROM sekolah s LEFT JOIN jurusan j ON j.id_sekolah = s.id_sekolah WHERE s.id_jenjang = '".$jenjang."' GROUP BY s.id_sekolah";
    $q   = $this->db->query($sql);
    return $q->result();
}

请帮助我。谢谢!

【问题讨论】:

  • 可以使用group_concat函数
  • @Chetan Ameta 好的,我试试

标签: php mysql codeigniter loops


【解决方案1】:

所以基本上你想为 TKL1、TKL2 显示 ol。如果是这样的话 首先使用group_concatj.nama_jurusan 喜欢

GROUP_CONCAT(DISTINCT j.nama_jurusan SEPARATOR ',') 

在您的查询中

然后在视图中分解来自, 的值,然后使用foreach 循环在li 中打印它

只是猜测,希望对您有所帮助。

【讨论】:

  • 我已经尝试过了,但错误消息显示“消息:未定义属性:stdClass::$nama_jurusan”
  • 请告诉我行号和文件名,显示有错误
  • 我像这样更改我的查询“SELECT s.nama_sekolah, s.alamat, s.no_telp, GROUP_CONCAT(DISTINCT j.nama_jurusan SEPARATOR ',') FROM sekolah s LEFT JOIN jurusan j ON j.id_sekolah = s.id_sekolah WHERE s.id_jenjang = '".$jenjang."' GROUP BY s.id_sekolah" 和错误"消息:未定义属性:stdClass::$nama_jurusan 文件名:ppdb/vlokasi.php 行号:26"
  • 请对数据库返回的对象使用 print_r 函数。看看输出是什么,如果有任何属性nama_jurusan,请告诉ppdb/vlokasi.php是否是你的视图文件?
  • 对不起,我忘了给它命名,所以出现了错误消息。我把它改成这样“GROUP_CONCAT(DISTINCT j.nama_jurusan SEPARATOR ',') AS nama_jurusan”。谢谢你的回答真的很有效。
猜你喜欢
  • 2015-05-10
  • 2020-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-17
  • 1970-01-01
  • 2020-10-08
  • 2019-06-29
相关资源
最近更新 更多