-3

我有两张桌子表topik和表剂量

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

但是有消息错误“数组到字符串的转换”......对不起,,,我几天前没有任何连接到互联网......

4

2 回答 2

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
于 2013-03-28T10:22:02.030 回答
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(); 

代码的最后一行将以对象数组的形式返回查询结果,失败时返回空数组。

我怀疑您正在尝试在需要使用字符串的地方使用此数组。由于尝试将数组解析为字符串,因此您可能会收到此错误。

于 2013-03-30T12:44:38.017 回答