【问题标题】:How to INNER JOIN 3 tables using CodeIgniter如何使用 CodeIgniter 对 3 个表进行 INNER JOIN
【发布时间】:2011-11-18 09:58:20
【问题描述】:

谁能告诉我如何用 php 加入 3 个表? 示例

SELECT FROM table1, table2,table on INNERJOIN -------------------

让我有 3 个表。(问题表、答案表和类别表) 这是我网页的示例。

Time remaining 30 minutes(I will get "30 minutes" form Category table)
1. Question (from question table)
2. answer (from answer table)

我不知道如何加入 3 个表。

【问题讨论】:

  • 你是怎么做到的?使用硬编码的 SQL 语句?使用 ActiveRecord 之类的框架?还是别的什么?

标签: php mysql codeigniter


【解决方案1】:

应该是这样的,

$this->db->select('*');    
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id');
$this->db->join('table3', 'table1.id = table3.id');
$query = $this->db->get();

根据 CodeIgniters 活动记录框架

【讨论】:

    【解决方案2】:

    我相信使用 CodeIgniters 活动记录框架,您只需一个接一个地使用两个连接语句。
    例如:

    $this->db->select('*');
    $this->db->from('table1');
    $this->db->join('table1', 'table1.id = table2.id');
    $this->db->join('table1', 'table1.id = table3.id');
    $query = $this->db->get();
    

    试一试,看看效果如何。

    【讨论】:

    • 谢谢Klee,如果不是codeigniter,你能告诉我吗?如何用Sql命令连接三个表?对不起,我的英语使用不好。请耐心等待。
    【解决方案3】:

    我创建了一个函数来获取一个包含字段值的数组并加入。这在模型中:

      public function GetDataWhereExtenseJoin($table,$fields,$data) {
        //pega os campos passados para o select
        foreach($fields as $coll => $value){
            $this->db->select($value);
        }
        //pega a tabela
        $this->db->from($table);
        //pega os campos do join
        foreach($data as $coll => $value){
            $this->db->join($coll, $value);
        }
        //obtem os valores
        $query = $this->db->get();
        //retorna o resultado
        return $query->result();
    
    }
    

    这进入控制器:

    $data_field = array(
            'NameProduct' => 'product.IdProduct',
            'IdProduct' => 'product.NameProduct',
            'NameCategory' => 'category.NameCategory',
            'IdCategory' => 'category.IdCategory'
            );
        $data_join = array
                        ( 'product' => 'product_category.IdProduct = product.IdProduct',
                          'category' => 'product_category.IdCategory = category.IdCategory',
                          'product' => 'product_category.IdProduct = product.IdProduct'
                        );
        $product_category = $this->mmain->GetDataWhereExtenseJoin('product_category', $data_field, $data_join);
    

    结果:

    echo '<pre>';
        print_r($product_category);
        die;
    

    【讨论】:

      【解决方案4】:

      我认为在 CodeIgniter 中最好使用上面写的 ActiveRecord。 还有一件事:您可以在 AR 中使用方法链:

      $this->db->select('*')->from('table1')->join('table2','table1.id=table2.id')->...
      

      【讨论】:

        【解决方案5】:
        $this->db->select('*');    
        $this->db->from('table1');
        $this->db->join('table2', 'table1.id = table2.id','JOIN Type');
        $this->db->join('table3', 'table1.id = table3.id');
        $query = $this->db->get();
        

        这将为您提供 table1,table2,table3 的结果,您可以在 $this->db->join() 函数的第三个变量中使用任何类型的连接,例如 inner、left、right 等。

        【讨论】:

          【解决方案6】:

          用于执行纯 SQL 语句(我不知道 FRAMEWORK-CodeIGNITER !!!) 你可以使用SUB QUERY! 语法如下

          选择 t1.id 从示例 t1 INNER JOIN (从 (example2 t1 join example3 t2 on t1.id = t2.id) 中选择 id)作为 t2 ON t1.id = t2.id;

          希望你明白我的意思!

          【讨论】:

            【解决方案7】:
            $this->db->select('*');    
            $this->db->from('table1');
            $this->db->join('table2', 'table1.id = table2.id', 'inner');
            $this->db->join('table3', 'table1.id = table3.id', 'inner');
            $this->db->where("table1", $id );
            $query = $this->db->get();
            

            您可以在哪里指定应在特定表中查看或选择哪个 id。您还可以在 join 方法的第三个参数上选择左、右、外、内、左外和右外的连接部分。

            【讨论】:

              【解决方案8】:

              你可以像这样修改你的编码

               $this->db->select('a.nik,b.nama,a.inv,c.cekin,c.cekout,a.tunai,a.nontunai,a.id');
               $this->db->select('DATEDIFF (c.cekout, c.cekin) as lama');
               $this->db->select('(DATEDIFF (c.cekout, c.cekin)*c.total) as tagihan');
               $this->db->from('bayar as a');
               $this->db->join('pelanggan as b', 'a.nik = b.nik');
               $this->db->join('pesankamar_h as c', 'a.inv = c.id');
               $this->db->where('a.user_id',$id);
               $query = $this->db->get();
               return $query->result();
              

              我希望能解决你的 SQL

              【讨论】:

                【解决方案9】:
                function fetch_comments($ticket_id){
                    $this->db->select('tbl_tickets_replies.comments, 
                           tbl_users.username,tbl_roles.role_name');
                    $this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
                    $this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
                    $this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
                    return $this->db->get('tbl_tickets_replies');
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-03-13
                  • 2014-02-21
                  • 1970-01-01
                  • 2019-02-19
                  • 1970-01-01
                  • 2019-12-05
                  • 1970-01-01
                  相关资源
                  最近更新 更多