查询与笨

问题描述:

我有2个数据库,我想作一个查询与2个数据库,例如像查询与笨

SELECT base1.table1.item1 FROM base1.table1 INNER JOIN base2.table3 ON base2.table3.item2 = base1.table1.item2 WHERE base2.table3.item4 = 'toto'; 

如何使这个查询使用CodeIgniter多个数据库? 我已经在CodeIgniter中配置了2个数据库的database.php。

谢谢。

+0

稍微提一下前DB名称该表在你的查询 –

您可以设置2数据库中的config/database.php中文件

$active_group = 'default'; 
$query_builder = TRUE; 

$db['default'] = array(
'dsn' => '', 
'hostname' => 'localhost', 
'username' => 'root', 
'password' => '', 
'database' => 'first_db', 
'dbdriver' => 'mysqli', 
'dbprefix' => '', 
'pconnect' => FALSE, 
'db_debug' => (ENVIRONMENT !== 'production'), 
'cache_on' => FALSE, 
'cachedir' => '', 
'char_set' => 'utf8', 
'dbcollat' => 'utf8_general_ci', 
'swap_pre' => '', 
'encrypt' => FALSE, 
'compress' => FALSE, 
'stricton' => FALSE, 
'failover' => array(), 
'save_queries' => TRUE 
); 

//set second db configuration 
$db['otherdb'] = array(
'dsn' => '', 
'hostname' => 'localhost', 
'username' => 'root', 
'password' => '', 
'database' => 'second_db', 
'dbdriver' => 'mysqli', 
'dbprefix' => '', 
'pconnect' => FALSE, 
'db_debug' => (ENVIRONMENT !== 'production'), 
'cache_on' => FALSE, 
'cachedir' => '', 
'char_set' => 'utf8', 
'dbcollat' => 'utf8_general_ci', 
'swap_pre' => '', 
'encrypt' => FALSE, 
'compress' => FALSE, 
'stricton' => FALSE, 
'failover' => array(), 
'save_queries' => TRUE 

);

当你想使用默认的数据库是指主数据库

// use master dataabse 
$users = $this->db->get('users'); 

// connect to secondary database 
$otherdb = $this->load->database('otherdb', TRUE); 
$data = $otherdb->get('table_name'); 

,如果你的第一个数据库名是BASE1和第二是BASE2

$this->db->select('table1.item1 FROM table1'); 
       $this->db->from('table1'); 
       $this->db->join('base2.table3', 'base2.table3.item2 =table1.item2'); 
$this->where('base2.table3.item4','toto') 
$query = $this->db->get(); 
+0

但如何使SQL连接? – iAmoric

+0

请检查更新回答 –

+0

谢谢,我会试试! – iAmoric