How to inter link three mysql tables using Codeign

2019-08-24 05:03发布

问题:

I have the following three tables. I am trying to inter link (join) among these three tables like this-

link 1

table1.code=table2.account-code  
table1.code=table3.t-code      
table2.voucher_no=table3.voucher_no 

I tried to query in Codeignitier's way but I get an error message that says table1 is not unique or something like that.

here's what I tried(and got the error)

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

Would you please kindly show me where I did wrong?

回答1:

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

If you want to select all three table c my select statment ALso see how join are used i have removed brackets you used in joins.

EDITED

This will produce this query

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

And for testing purpose

http://brettdewoody.com/labs/active-check/index.php