我真的需要帮助。我必须加入两个数据库,并且我已在查询中尝试并在 Controller 中的本地模型中实现,并且此代码完美运行。这是我的 sql 查询:
SELECT skapp.p.*, c1db_pegawai.e.eselon, c1db_pegawai.j.nama_jenis FROM skapp.ref_tunjangan_struktural p
JOIN c1db_pegawai.r_eselon e on e.id = p.eselon_id
JOIN c1db_pegawai.r_jenis_pegawai j on j.id = p.jenis_pegawai_id
我在 Codeigniter 中实现并将其添加到我的代码中:
$query = $this->db->query($sql);
在我看来,本地主机也工作得很好。这是可行的,因为我的本地有两个数据库,并在一个本地主机中查询所有这两个数据库。但在我的实时服务器中,我有两个位于不同位置的数据库。我已经完成了连接数据库配置,如 Codeigniter 文档中所述。我也在我的实时服务器上尝试过这个,这是我模型中的代码:
$this->db->select('p.* as p');
$this->another->select('e.eselon as e, j.nama_jenis as j');
$this->db->from('ref_tunjangan_struktural');
$this->another->join('r_eselon','r_eselon.id = ref_tunjangan_struktural.eselon_id');
$this->another->join('r_jenis_pegawai','r_jenis_pegawai.id = ref_tunjangan_struktural.jenis_pegawai_id');
$this->db->order_by("ref_tunjangan_struktural.id", "asc");
这是我的数据库连接 Controller :
$this->load->database('default',TRUE); //first DB
$this->another = $this->load->database('pegawai',TRUE); //second DB
但我的实时服务器上出现错误结果。
$这个->另一个
是我对第二个数据库的查询。但还是有错误。请问有人可以帮助我修复我的实时服务器模型吗?
这是我的错误吗?
Error Number: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'as p FROM ref_tunjangan_struktural as p ORDER BY ref_tunjangan_struktural' at line 1 SELECT p.* as p` FROM ref_tunjangan_struktural as p ORDER BY ref_tunjangan_struktural.id ASC
最佳答案
你不能这样做 - 你不能混合 2 个 QueryBuilder 对象来创建一个查询
The only thing you can do is to use your first DB instance in order to connect to the other database - but this has one condition - your User from your first DB has to have access to the second one - there is simply no other possibility
在给定条件下 - 以下内容应该有效
$query = $this->db
->select('skapp.p.*,c1db_pegawai.e.eselon, c1db_pegawai.j.nama_jenis')
->from('skapp.ref_tunjangan_struktural p')
->join('c1db_pegawai.r_eselon e', 'e.id = p.eselon_id')
->join('c1db_pegawai.r_jenis_pegawai j', 'j.id = p.jenis_pegawai_id')
->get();
print_r($query->result());
关于php - 将mysql中的查询转换为两个数据库中的Codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52126053/