duanpei8853 2013-01-04 15:04
浏览 58
已采纳

在codeigniter中加入两个数据库的查询

I need to write a join query of two tables from two databases and fetch the joined data. For eg, consider I have a database db1 which has some tables named companies, plans, customers. Suppose I need to join the two tables companies and plans with another table 'cdr' on another database db2 by grouping them using a similar column.

The query which I'm running right now is given below:

function get_per_company_total_use ($custid)
        {         
                 $this->DB1->select('ph_Companies.CompanyName');
                 $this->DB1->where('ph_Companies.Cust_ID', $custid);
                 $this->DB2->select_sum('cdr.call_length_billable')->from('cdr');
                 $this->DB2->group_by('cdr.CompanyName');
                 $this->db->join('Kalix2.ph_Companies', 'Kalix2.ph_Companies.CompanyName = Asterisk.cdr.CompanyName');
                 $query = $this->db->get();
                 if($query->result()){
                     foreach ($query->result() as $value) {
                         $companies[]= array($value->CompanyName,$value->call_length_billable);
                          }
                     return $companies;
                 }
                 else 
                     return FALSE;
        }

But my query is not fetching the data and throwing an error. This same query, I have run on a single database and is working. But I need help to find how this can be done with two databases.

  • 写回答

2条回答 默认 最新

  • douyan4470 2013-01-04 15:45
    关注

    You can just give the following if you need to join two database tables:

    function get_per_company_total_use ($custid)
            {         
                     $this->db->select('Kalix2.ph_Companies.CompanyName');
                     $this->db->where('Kalix2.ph_Companies.Cust_ID', $custid);
                     $this->db->select_sum('Asterisk.cdr.call_length_billable')->from('Asterisk.cdr');
                     $this->db->group_by('Asterisk.cdr.CompanyName');
                     $this->db->join('Kalix2.ph_Companies', 'Kalix2.ph_Companies.CompanyName = Asterisk.cdr.CompanyName');
                     $query = $this->db->get();
                     if($query->result()){
                         foreach ($query->result() as $value) {
                             $companies[]= array($value->CompanyName,$value->call_length_billable);
                              }
                         return $companies;
                     }
                     else 
                         return FALSE;
            }
    

    Here actually you need not give the connection variable DB1 or DB2, just give $this->db.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵