我正在使用 Jamie Rumbelow的MY_model ,我正试图弄清楚如何通过连接从第二个表中获取单个值。我想要做的是获取所有用户并进行一个将检索status_name的连接,这样当我可以显示查询结果时我可以做到
foreach ($users AS $user) {
echo $user->status_name;
}
有关如何操作的任何想法?
user_id username status_id role_id
-------------------------------------------
1 testuser1 1 (active) 1 (guest)
2 testuser2 1 2 (user)
3 testuser3 1 3 (editor)
4 testuser4 1 4 (admin)
status_id status_name
-----------------------
1 active
2 inactive
3 suspended
4 banned
5 deleted
答案 0 :(得分:0)
试试这个
$this->db->select('table1.user_id, table1.username, table2.status_name, table1.role_id');
$this->db->from('table1');
$this->db->join('table2', 'table2.status_id = table1.status_id');
$result = $this->db->get();
或者
$this->load_table('table1');
$this->load_table('table2');
$this->model_name->related('table2', array('status_id', 'status_id'));
$user=$this->model_name->get(
array(
'feilds'=>array(user_id,username,status_name,role_id)
)
);