我有以下三个表。我正在尝试像这样在这三个表之间进行互连(联接)-
链接1
table1.code=table2.account-code
table1.code=table3.t-code
table2.voucher_no=table3.voucher_no
我尝试以 Codeignitier 的方式进行查询,但收到一条错误消息,指出 table1 不唯一或类似的内容。
这是我尝试过的(并收到错误)
$this->db->select('*');
$this->db->from('table2');
$this->db->join('table3', 'table2.voucher_no = table3.voucher_no');
$this->db->join('table1', '(table1.code = table2.account_code)');
$this->db->join('table1', '(table1.code = table3.t_code)');
请您告诉我我哪里做错了?
最佳答案
$this->db->select('table1.* , table2.* , table3.*');
$this->db->from('table2');
$this->db->join('table3', 'table2.voucher_no = table3.voucher_no','left');
$this->db->join('table1', 'table1.code = table2.account_code AND table1.code = table3.t_code','left');
$this->db->get();
如果你想选择所有三个表c我的选择语句 还要看看如何使用连接,我已经删除了您在连接中使用的括号。
已编辑
这将产生这个查询
SQL: SELECT `table1`.*, `table2`.*, `table3`.* FROM (`table2`) LEFT JOIN `table3` ON `table2`.`voucher_no` = `table3`.`voucher_no` LEFT JOIN `table1` ON `table1`.`code` = `table2`.`account_code` AND table1.code = table3.t_code
出于测试目的
关于php - 如何使用 Codeigniter 互连三个 mysql 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10053670/