【问题标题】:How to inter link three mysql tables using Codeigniter如何使用 Codeigniter 链接三个 mysql 表
【发布时间】:2012-04-20 16:08:45
【问题描述】:

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

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

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

【问题讨论】:

    标签: php mysql codeigniter codeigniter-2


    【解决方案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();
    

    如果你想选择所有三个表 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

    【讨论】:

      猜你喜欢
      • 2011-10-16
      • 2011-04-12
      • 2021-06-25
      • 2012-08-08
      • 1970-01-01
      • 2014-02-24
      相关资源
      最近更新 更多