Join query of two databases in codeigniter

后端 未结 2 1226
旧时难觅i
旧时难觅i 2020-12-11 05:47

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,

2条回答
  •  春和景丽
    2020-12-11 05:57

    I'm having table x in DB1 which contain id, name and y_id. In DB2 there is a y table with id and name

    so if you need to get name of x and y in a same query the here is your answer:

        $db = $this->load->database("DB2", TRUE);
        $DB = $this->load->database("DB1", TRUE);
        $DB->select('x.name as name1, y.name as name2');
        $DB->from('x');
        $DB->join($db->database.'.y','x.y_id = y.id');
        $res = $DB->get();
    

提交回复
热议问题