Codeigniter多数据库联接 [英] Codeigniter multiple database join

查看:146
本文介绍了Codeigniter多数据库联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用codeigniter框架.我在这个框架上经验不足.我需要你们的帮助.我想加入存在于两个不同数据库中的两个表.在这两个表中,我都有一个公共列.我了解,首先我必须在文件database.php内创建一个单独的数据库组. 我已经创建了该组,并且能够在我的模型中单独使用该组.我还在模型默认组"中加载了另一个组.每当我尝试单独使用每个组时,它都可以正常工作.但是我在努力使用这两个数据库组在两个数据库上进行联接.

I am using codeigniter framework. I am not much experienced in this framework. I need help of you guys. I want to join two table present in two different database. In both the table I have one common column. I understand, first I have to create a separate database group inside the file database.php. I have created the group and am able to use ths group in my model separately. I have also loaded another group in my model "default group". Whenever, I am trying to use each group separately it's working without any issue. But I am struggling how to make join on two database using these two database group.

现在,我想使用这两个单独的组来连接不同数据库的两个表.但是我不确定我到底在哪里犯错.

Now I want to join two tables of different database using these two separate group. But I am not sure where exactly I am doing the mistake.

这是我的模型文件.

class Bar_graph extends CI_Model {

                public function __construct () {
                     parent::__construct();
                     $this->db=   $this->load->database('default', TRUE);//This is the default group
                     $this->db2 = $this->load->database('db2', TRUE); //This is the new group I have created
                 }
 //kalix2 and Asterik are my two different database               
               public function join_two_database ()
                {    
                      $cust_id=2;
                      $this->db->select('Kalix2.ph_Companies.CompanyName');
                      $this->db2->select_sum('Asterik.cdr.call_length_billable');
                      $this->db2->select('Asterik.cdr.calldate');
                      $this->db->where('Kalix2.ph_Companies.Cust_ID',$cust_id);
                      $this->db->from('Kalix2.ph_Companies');
                      $this->db2->group_by('Asterik.cdr.CompanyName');
                      $this->db->limit(5);
                      $this->db->join('Asterik.cdr','Kalix2.ph_Companies.CompanyName = Asterik.cdr.CompanyName','inner');
                      $query = $this->db->get();
                     if ($query->num_rows > 0) {
                     return $query-> result(); 
                    }
                }

推荐答案

AFAIK,您不能在一个语句中加入它们.一次只能与一个数据库建立一个连接.您必须在一个数据库上运行查询,在第二个数据库上运行查询,然后在PHP中操纵这两者的结果.

AFAIK, you can't join these in one statement. You only have one connection to a database at a time. You'd have to run a query on one database, run a query on the second database and then manipulate the results of both within PHP.

这篇关于Codeigniter多数据库联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆