在 CodeIgniter 中连接来自 2 个不同数据库中的 2 个表的查询



我需要连接 2 个数据库。

这是我的查询,但$db2仅适用于第二个数据库,$this->db->query仅适用于第一个数据库。

$akses = $db2->query("select A.uName, A.userPwd, A.[Nomor Induk], B.id_jabatan, B.nama FROM tUser A
LEFT JOIN karyawan B ON B.nik = A.[Nomor Induk]
LEFT JOIN bagian_departemen C ON C.id_bagian_dept = B.id_bagian_dept
WHERE A.uName = '$username' AND A.userPwd = '$password'"
);

我想知道从 2 个数据库中连接 2 个表的语法。

数据库:

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'helpdesk',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'striction' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['another_db'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'produksi',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'striction' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

尝试如下:

$this->db->select("db1.table.column")
$this->db->join('db1.table', 'db1.table.column = db2.table.column');
$query = $this->db->get();

相关内容

  • 没有找到相关文章

最新更新