问题描述
我有以下三个表。我试图在这三个表之间互相链接(加入),像这样 -
链接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)');
$ it-> 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我选择statment
还看看join如何
这将产生此查询
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`和table1.code = table3.t_code
并用于测试目的
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?
$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 statmentALso 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
这篇关于如何使用Codeigniter互连三个mysql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!