php - 如何使用 Codeigniter 互连三个 mysql 表

标签 php mysql codeigniter codeigniter-2

我有以下三个表。我正在尝试像这样在这三个表之间进行互连(联接)-

链接1

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

enter image description here

我尝试以 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

出于测试目的

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

关于php - 如何使用 Codeigniter 互连三个 mysql 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10053670/

相关文章:

当数据库中不存在所选数据时,PHP undefined variable

php - zend - 从 Controller 连接到数据库

mysql - Google Analytics Reporting API 与查询参数

php - 如何循环遍历 PHP 代码以将 JSON 数据插入 MySQL 数据库

php - 如何在 codeigniter 中将多维数组作为单独的行保存到数据库中?

PHP LDAP : How to search if user is in group?

php - 在 View laravel 5 中包含 Controller

PHP - 在 while 循环中分配值并在循环外单独访问它们

php - 在 PHP 中加密和解密密码、文件的完美方法?

javascript - 如何使用 JavaScript 在 google map (1 个或多个)中绘制形状