简体   繁体   中英

SQL multiple join does not return all rows in Codeigniter

This is my query:

$this->db->select('e.emp_no', 'e.birth_date', 'e.first_name', 'e.last_name', 'e.gender', 'e.hire_date')
            ->from('employees AS e')
            ->join('titles AS t', 'e.emp_no = t.emp_no')
            ->join('salaries AS s', 't.emp_no = s.emp_no')
            ->join('dept_emp AS de', 's.emp_no = de.emp_no')
            ->join('departments AS d', 'de.dept_no = d.dept_no')
            ->join('dept_manager AS dm', 'd.dept_no = dm.dept_no')
            ->limit($limit, $offset)
            ->order_by($sort_by, $sort_order)
            ->group_by('e.emp_no')
            ->where('t.title', 'staff'); 

I then return the array with this:

$ret['rows'] = $q->get()->result();

        print_r ($ret['rows']);

All that is outputted is the emp_no:

Array ( [0] => stdClass Object ( [emp_no] => 10002 ) [1] => stdClass Object ( [emp_no] => 10005 ) [2] => stdClass Object ( [emp_no] => 10007 ) [3] => stdClass Object ( [emp_no] => 10011 ) )

How do I get to output all the selected columns?

PS: I have also tried to use result_array() nothing has worked.

You should combine all field names into one string instead of separating them as multiple parameters to select() function

$this->db->select('e.emp_no, e.birth_date, e.first_name, e.last_name, e.gender, e.hire_date')
        ->from('employees AS e')
        ->join('titles AS t', 'e.emp_no = t.emp_no')
        ->join('salaries AS s', 't.emp_no = s.emp_no')
        ->join('dept_emp AS de', 's.emp_no = de.emp_no')
        ->join('departments AS d', 'de.dept_no = d.dept_no')
        ->join('dept_manager AS dm', 'd.dept_no = dm.dept_no')
        ->limit($limit, $offset)
        ->order_by($sort_by, $sort_order)
        ->group_by('e.emp_no')
        ->where('t.title', 'staff'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM