简体   繁体   中英

Join Query from 2 Table in 2 Different Databases in CodeIgniter

I have 2 databases needs to be joined.

Here is the query that I have but the $db2 is only for the 2nd database, and $this->db->query only for 1st database.

     $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'"
                            );

I want to know the syntax to join 2 table from 2 databases.

Databases:

$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
);

try like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM