【问题标题】:Codeigniter: Select from multiple tablesCodeigniter:从多个表中选择
【发布时间】:2011-02-16 00:02:32
【问题描述】:

如何从两个或多个表中选择行?

我正在为表单设置默认字段,我需要来自两个表的值...

我当前的代码如下:

    $this->CI->db->select('*');
    $this->CI->db->from('user_profiles');
    $this->CI->db->where('user_id' , $id);
    $user = $this->CI->db->get();
    $user = $user->row_array();
    $this->CI->validation->set_default_value($user);

【问题讨论】:

    标签: activerecord select codeigniter


    【解决方案1】:

    用户指南中的示例应说明这一点:

    $this->db->select('*'); // <-- There is never any reason to write this line!
    $this->db->from('blogs');
    $this->db->join('comments', 'comments.id = blogs.id');
    
    $query = $this->db->get();
    
    // Produces:
    // SELECT * FROM blogs
    // JOIN comments ON comments.id = blogs.id
    

    查看用户指南中Active Record 页面下的全部内容。

    【讨论】:

    • 用户指南中的示例似乎没有从第二个表中生成字段。你是怎么做到的?
    • * = 一切,来自所有可用的表。如果它没有出现,你做错了什么。此外,如果您在不同的表中有两个具有相同名称的字段,则只会显示一个。您需要执行foo as bar 才能获得-&gt;bar
    【解决方案2】:

    只需将另一个表添加到“->from()”方法即可。比如:

     $this->db->select('t1.field, t2.field2')
              ->from('table1 AS t1, table2 AS t2')
              ->where('t1.id = t2.table1_id')
              ->where('t1.user_id', $user_id);
    

    【讨论】:

    • 这会产生语法错误。
    • 不在我这边。你能详细说明一下吗?
    • 只有php5支持该语法
    【解决方案3】:

    我认为问题不在于连接,而在于如何显示来自两个不同表的值 - 用户指南似乎没有解释这一点。

    这是我的看法:

        $this->db->select('u.*, c.company, r.description');
        $this->db->from('users u, company c, roles r');
        $this->db->where('c.id = u.id_company');
        $this->db->where('r.permissions = u.permissions');
        $query = $this->db->get();
    

    【讨论】:

    • 这正是我需要看到的。不确定逗号分隔的字符串或具有多个值的数组是否是正确的语法。也不确定是否在 where 方法中匹配它们。
    • 如果需要记录集可以这样做:return $this->db->get()->result();
    【解决方案4】:

    我认为语法不正确。 您需要选择一条记录。我有两个表,我有一个表的 id 参数传递,以及两个表的关系。

    【讨论】:

      【解决方案5】:

      试试这个

         $this->db->select('*')
                  ->from('student')
                  ->where('student.roll_no',$id)
                  ->join('student_details','student_details.roll_no = student.roll_no')
                  ->join('course_details','course_details.roll_no = student.roll_no');
         $query = $this->db->get();
         return $query->row_array();
      

      【讨论】:

        【解决方案6】:

        //从表1中选择所有字段,从表2中选择一个或多个字段......

        $this->db->select('table1.*, table2.name');
            $this->db->from('table1, table2');
            $this->db->where('table2.category_id = table1.id');
            $this->db->where('table2.lang_id',$id); // your where with variable
            $query = $this->db->get();
            return $query->result();
        

        【讨论】:

          【解决方案7】:
          $SqlInfo="select a.name, b.data fromtable1 a, table2 b where a.id=b.a_id";
          $query = $this->db->query($SqlInfo);
          

          试试这个方法,你可以添加第三个名为c的表,并在sql命令中添加'and'命令。

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-28
          • 2011-09-01
          • 1970-01-01
          • 2012-08-23
          • 1970-01-01
          • 2019-03-05
          • 1970-01-01
          • 2015-07-29
          相关资源
          最近更新 更多