Codeigniter:从多个表中选择

Kev*_*own 7 activerecord select codeigniter

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

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

我目前的代码是:

    $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);
Run Code Online (Sandbox Code Playgroud)

Phi*_*eon 20

用户指南中的示例应解释此:

$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
Run Code Online (Sandbox Code Playgroud)

请参阅" 用户指南"中" Active Record"页面下的整个内容.


Fav*_*vio 11

只需将另一个表添加到" - > 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);
Run Code Online (Sandbox Code Playgroud)


Joh*_*and 8

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

这是我的看法:

    $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();
Run Code Online (Sandbox Code Playgroud)