0

我有以下三个表。我正在尝试像这样在这三个表之间相互链接(加入)-

链接 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)');

请你告诉我我哪里做错了吗?

4

1 回答 1

2
$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

并出于测试目的

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

于 2012-04-07T10:38:06.233 回答