在codeigniter中加入两个数据库的查询 [英] Join query of two databases in codeigniter

查看:44
本文介绍了在codeigniter中加入两个数据库的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个来自两个数据库的两个表的连接查询并获取连接的数据.例如,考虑我有一个数据库 db1,其中有一些名为公司、计划、客户的表.假设我需要通过使用相似的列对它们进行分组来连接两个表公司并计划与另一个数据库 db2 上的另一个表 'cdr'.

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.

推荐答案

如果你需要连接两个数据库表,你可以只给出以下内容:

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;
        }

这里其实不需要给连接变量DB1或DB2,只要给$this->db即可.

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

这篇关于在codeigniter中加入两个数据库的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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