简体   繁体   English

如何使用 Codeigniter 链接三个 mysql 表

[英]How to inter link three mysql tables using Codeigniter

I have the following three tables.我有以下三个表。 I am trying to inter link (join) among these three tables like this-我正在尝试像这样在这三个表之间建立链接(加入)-

link 1链接 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.我尝试以 Codeignitier 的方式进行查询,但收到一条错误消息,指出 table1 不是唯一的或类似的东西。

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 statment ALso see how join are used i have removed brackets you used in joins.如果你想要 select 所有三个表 c 我的 select 声明 ALso 查看如何使用连接我已经删除了你在连接中使用的括号。

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 http://brettdewoody.com/labs/active-check/index.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM