搜索,识别主键并从多个表返回结果

时间:2013-01-04 17:46:42

标签: php codeigniter activerecord

在我的代码中,我只能搜索一个表并从一个表中返回一些内容,但是从发布的值姓氏中,如何获得'emp_no'以便我可以从'dept_emp'和'返回'dept_no'标题'来自'标题'?

控制器

public function findemp() 
{   
    // search in table 'employees'
    $name = $this->input->get('lastname');


    if($data['user'] = $this->fndmodel->finduser($name)) {

        $this->load->view('searchview', $data);
    }

    // search in table 'dept_emp'
    $dept = $this->input->get('deptmnt');
    // search in table 'titles'
    $title = $this->input->get('title');
}

模型

 public function finduser($name) {

    if ($name == null || $name == '') {
            return false;
    }

    $this->db->select('e.emp_no, e.first_name, e.last_name, t.title, d.dept_no');
    $this->db->from('employees AS e, titles AS t, dept_emp AS d');
    $this->db->where('e.emp_no = t.emp_no');
    $this->db->where('e.emp_no = d.emp_no');
    $this->db->where('e.last_name', $name);
    $data['query'] = $this->db->get();              
    return $data;

}    

这就是我在activerecords中的意图

SELECT e.emp_no, e.first_name, e.last_name, t.title, d.dept_no
FROM employees e JOIN titles t
ON (e.emp_no = t.emp_no)
JOIN dept_emp d
ON (e.emp_no = d.emp_no)
WHERE e.last_name = "facello";

这是对的吗?

$this->db->select('e.emp_no, e.first_name, e.last_name, t.title, d.dept_no');
$this->db->from('employees AS e, titles AS t, dept_emp AS d');
$this->db->where('e.emp_no = t.emp_no');
$this->db->where('e.emp_no = d.emp_no');
$this->db->where('e.last_name', $name);
$result = $this->db->get();

0 个答案:

没有答案