我正在尝试基于userid加入两个表。我有两个表pr_resignation_requests(列名:userids)和pr_users_details(列名:userid)。我想在userid = ".$val."
的两个表中选择所有的entires。我收到了这个错误:
错误号码:1248
每个派生表都必须有自己的别名
SELECT * FROM pr_resignation_requests
LEFT JOIN(SELECT firstname,lastname FROM pr_users_details WHERE userid = 81
)
我的控制器代码是:
function view_exit_details(){
global $SITE,$USER;
$data = array();
$data['row'] = new stdClass();
$data['row'] = $this->admin_init_elements->set_post_vals($this->input->post());
$data['error_message'] = '';
$data['row']->id = $data['id'] = $this->uri->segment(3);
$data['action'] = 'add';
$data['heading'] = 'Add';
$data['msg_class'] = 'sukses';
$data['path']=$path;
$post_action = $this->input->post('action');
if($post_action=='add' || $post_action =='update' ){
$post_array = $this->input->post();
$action = ($post_action == 'add')?'inserted':'updated';
$data['error_message'] = $this->exit_common->add_edit_attendance_issue($post_array,$action);
}
if($data['id']>0){
$data['rows']=$this->exit_common->get_cat_array2('pr_resignation_requests','id',$data['id'],'');
$data['action'] = 'update';
}
$this->data['maincontent'] = $this->load->view('maincontents/view_resignation_action', $data,true);
$this->load->view('layout', $this->data);
}

我的模型代码是:
function get_cat_array2($table_name,$col=NULL,$val=NULL,$orderby=NULL){
$result=array();
$sql = "SELECT * FROM `".$table_name."` LEFT JOIN(SELECT firstname,lastname FROM pr_users_details WHERE userid = `".$val."` )";
$query=$this->db->query($sql);
if($query->num_rows() > 0){
$result=$query->result();
}
return $result;
}

答案 0 :(得分:0)
使用下一个代码段:
SELECT *
FROM pr_resignation_requests
LEFT JOIN
(
SELECT firstname,lastname FROM pr_users_details WHERE userid = 81;
) AS a ON (add u r code of primary key checking)