dosen table---->|dosen_id|dosen_nama| topik---->|dosen_id1|dosen_id2|
1 a 1 2
2 b 2 1
3 c 1 3
我希望计算行dosen_id1和dosen_id2作为这样的结果
|dosen_nama|count|
a 3
b 2
c 1
我必须写的模型中的代码是什么? 请帮帮我......
更新
这是我的控制器
$data['jumlah'] = $this->topikdo_model->get_all_topikdo_jumlah();
这是我的观点
<table class="table table-bordered table-striped table-hover ">
<tbody>
<tr>
<th>SANDOS</th>
<th>Nama</th>
<th>Jumlah Bimbingan</th>
<th>Aksi</th>
</tr>
<?php
$no=1;
foreach($daftardosen as $row)
{
?>
<tr>
<td><?php echo $row->dosen_id;?></td>
<td><?php echo $row->dosen_nama;?></td>
<td><?php echo $jumlah;?></td>
<td align='center'>
<?php
echo anchor('topikdo_controller/rincian_daftardosen/'.$row->dosen_id,'Rincian');
?>
</td>
</tr>
<?php $no++;
}
?>
</tbody>
</table>
我在像rohini sugested这样的模型中写了查询 模型
$sql="SELECT d.dosen_nama, count(t.a)
FROM dosen as d, (Select topik_pembimbing1 as a
FROM topik
UNION ALL
SELECT topik_pembimbing2 as a
FROM topik) as t
WHERE d.dosen_id = t.a
GROUP BY d.dosen_nama";
$data=$this->db->query($sql);return $data->result();
但是有消息错误“数组到字符串转换”... 对不起,,,几天前我没有任何连接到互联网......
答案 0 :(得分:0)
您只需执行查询即可获得所需的结果。
SELECT dosen_nama, ((SELECT COUNT(*) FROM topik WHERE dosen_id1=d.dosen_id) + (SELECT COUNT(*) FROM topik WHERE dosen_id2=d.dosen_id)) AS COUNT
FROM dosen d
答案 1 :(得分:0)
尝试在模型中使用此查询.......
SELECT d.dosen_nama, count(t.a)
FROM dosen as d, (Select dosen_id1 as a
FROM topik
UNION ALL
SELECT dosen_id2 as a
FROM topik) as t
WHERE d.dosen_id = t.a
GROUP BY d.dosen_nama
<强>更新强>
你写过,
$sql="SELECT d.dosen_nama, count(t.a)
FROM dosen as d, (Select topik_pembimbing1 as a
FROM topik
UNION ALL
SELECT topik_pembimbing2 as a
FROM topik) as t
WHERE d.dosen_id = t.a
GROUP BY d.dosen_nama";
$data=$this->db->query($sql);
return $data->result();
代码的最后一行将以对象数组的形式返回查询结果,或者在失败时返回空数组。
我怀疑你正在尝试使用这个数组,你需要使用一个字符串。 由于尝试将数组解析为字符串,因此可能会出现此错误。