在数据库Codeigniter中加入三个表

时间:2016-05-23 12:29:31

标签: php database codeigniter join

我有三个表case,user,user_role,在case表中有user_id和user_role_id,我想加入两个表users和user_role with cases将user_id转换为他在users表中的名字,并将user_role_id转换为user_role中的名字表,我在其中一个成功,但当加入另一个表时我发誓

in models / cases_model.php

function get_all()
    {

    $this->db->where('C.is_archived',0);
    $this->db->select('C.*,U.name client');
        $this->db->join('users U', 'U.id = C.client_id', 'LEFT');
        $this->db->join('user_role UR', 'UR.id = C.city_case', 'LEFT');
    return $this->db->get('cases C')->result();

    }

in controllers / cases.php

function index(){

        $data['cases'] = $this->cases_model->get_all();
        $data['courts'] = $this->cases_model->get_all_courts();
        $data['clients'] = $this->cases_model->get_all_clients();
        $data['locations'] = $this->location_model->get_all();
        $data['stages'] = $this->case_stage_model->get_all();
        $data['city'] = $this->cases_model->get_name_city();
        $data['page_title'] = lang('case');
        $data['body'] = 'case/list';
        $this->load->view('template/main', $data);  
    }

在view / list.php中

<?php if(isset($cases)):?>
     <tbody>
      <?php $i=1;foreach ($cases as $new){?>
       <tr class="gc_row">
      <td><?php echo $i?></td>

        <td class="small-col">                          
        <?php if($new->is_starred==0){ ?>
        <a href="" at="90" class="Privat"><span style="display:none"><?php echo $new->id?></span>
        <i class="fa fa-star-o"></i></a>
        <?php 
            }else{
            ?>
        <a href="" at="90" class="Public"><span style="display:none"><?php echo $new->id?></span>
        <i class="fa fa-star"></i></a>
        <?php
         }?>
        </td>
        <td><?php echo $new->title?></td>
        <td><?php echo $new->case_no?></td>
        <td><?php echo $new->client?></td>
        <td><?php echo $new->city_case?></td>

2 个答案:

答案 0 :(得分:0)

我不是在这里做sql部分。但如果CI的方法阻碍了你的任务,那么你应该使用简单的解决方案而不是CI的PDO方法。

简单的单词使用

 $this->db->query({some complex query as String});

答案 1 :(得分:0)

您可以尝试此查询以获得所需的输出

160523 13:07:29 [ERROR] Slave SQL: Error 'Duplicate entry '914166' for key
'PRIMARY'' on query. Default database: 'zo_dev_20121216'. Query: 'UPDATE    
   activity
SET
   activity_type_id = 9,
   subject = 'Send departure email',
   date_due = '2016-05-26 01:00',
   date_start = '2016-05-23 01:00',
   activity_status_id = 1,
   content = 'Send departure email'
WHERE
   id = 2888555', Error_code: 1062
160523 13:07:29 [Warning] Slave: Duplicate entry '914166' for key 'PRIMARY'     
Error_code: 1062
160523 13:07:29 [ERROR] Error running query, slave SQL thread aborted. Fix     
the problem, and restart the slave SQL thread with "SLAVE START". We stopped   
at log 'mysql-bin.000004' position 14847360

现在,在这里加入user_id被认为是主键..还有一件事你可以用所需的列名替换*!

谢谢..