donmqryh49993 2016-06-25 05:26
浏览 45

如何从关系数据库codeigniter获取数据

As i was using comma separated values in DB every one suggested me to normalize table so i normalized my table now my table looks like this

tuitor table
tuitor_id | name | mobile |etc
   1      | xyz  |14562547|anyinputs
   2      |abc   |47545875|text
   3      |abd   |47854874|yyy

now my normalized table is tuitor_keyword

tuitor_keyword_id | id_tuitor | id_keyword
   1              |   1       |123
   2              |   1       |124
   3              |   2       |123
   4              |   2       |155
   5              |   1       |187  

while retrieving any tuitor i want keywords in comma like for id_tuitor=1 it should be 123,124

this is the query what i am using

        $this->db->select('*')->from('tuitor');  
        $this->db->join('tuitor_keyword','tuitor.tuitor_id =  tuitor_keyword.id_tuitor','left');

  $query=$this->db->get();
        return $result = $query->result_array();

It is creating new array for for tuitor_keyword_id what should i do please correct me i know i have done plenty of mistakes

  • 写回答

1条回答 默认 最新

  • doutangshuan6473 2016-06-25 08:08
    关注

    CONCAT_GROUP is a solution for you:

    $this->db->query('SELECT id_tuitor, CONCAT_GROUP(id_keyword) AS gr FROM tuitor_keyword GROUP BY id_tuitor');
    

    or:

    $this->db->select('id_tuitor, CONCAT_GROUP(id_keyword) AS gr', false)->from('tuitor_keyword')->group_by('id_tuitor');
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类