0

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
);
  • 3
    Does this answer your question? [Can we use join for two different database tables?](https://stackoverflow.com/questions/11426929/can-we-use-join-for-two-different-database-tables) – mastazi Feb 03 '20 at 04:10
  • is it capable syntax for codeigniter? – Perdana Feb 03 '20 at 06:50
  • You tagged the question as SQL-Server, so if your Codeigniter is connected to a Microsoft SQL server, then yes it's valid, just use a raw query like in your first example. If you are using MySQL, then you need to change the tags in your question and that solution will not work for you – mastazi Feb 04 '20 at 01:47

1 Answers1

0

try like this:

$this->db->select("db1.table.column")
$this->db->join('db1.table', 'db1.table.column = db2.table.column');
$query = $this->db->get();
udit rawat
  • 221
  • 1
  • 5
  • do we need to create connection to first and second database? – Perdana Feb 03 '20 at 07:02
  • 1
    no, you don't need to define another connection. db1 is your actual database name and db2 is your another actual database name in mysql. – udit rawat Feb 03 '20 at 12:48
  • i tried this syntax $this->db->select("produksi.tUser.uName, produksi.tUser.userPwd, produksi.tUser[Nomor Induk], helpdesk.karyawan.id_jabatan, helpdesk.karyawan.nama"); $this->db->join('helpdesk.karyawan', 'helpdesk.karyawan.nik = produksi.tUser.[Nomor Induk]'); $this->db->join('bagian_departemen', 'helpdesk.bagian_departemen.id_bagian_dept = helpdesk.karyawan.id_bagian_dept'); $query = $this->db->get(); but error 42000 is appear. – Perdana Feb 04 '20 at 06:53
  • *[Nomor Induk]* why this, your query creating syntax error. print your last query and check where is syntax wrong. – udit rawat Feb 04 '20 at 12:00
  • [Nomor Induk] is the name of the column in table tUser – Perdana Feb 05 '20 at 06:30
  • put this column between `\`Nomor Induk\`` and check. and its a bad idea to have space for table column name. you can use underscore. – udit rawat Feb 05 '20 at 06:53