Разработка SQL-заявления: как присоединиться к одной таблице

0

PHP:

$this->db->select('employee.*, area.Name as Area, city.Name as City, state.Name as State, group_concat(DISTINCT employee_image.Image) as Image');
$this->db->from('employee');
$this->db->join('area', 'area.ID = employee.Area', 'LEFT');
$this->db->join('city', 'city.ID = employee.City', 'LEFT');
$this->db->join('state', 'state.ID = employee.State', 'LEFT');
$this->db->join('employee_image', 'employee_image.Employee = employee.ID', 'LEFT');
$this->db->group_by('employee.ID');
return $this->db->get()->result_array();

Я хочу присоединиться к самой таблице. Существует столбец Created_by там, где значение id сотрудника теперь я хочу присоединиться к себе и хочу получить первое и последнее имя из таблицы сотрудников, кто-нибудь может мне помочь, как я могу это сделать?

Теги:

1 ответ

0

Вы можете сделать что-то вроде

$this->db->select('e.*, a.Name as Area, c.Name as City, s.Name as State, group_concat(DISTINCT i.Image) as Image, e1.first_name createby_first_name , e1.last_name createdby_last_name ');
$this->db->from('employee e');
$this->db->join('employee as e1', 'e.Created_by  = e1.ID', 'LEFT');
$this->db->join('area a', 'a.ID = e.Area', 'LEFT');
$this->db->join('city c', 'c.ID = e.City', 'LEFT');
$this->db->join('state s', 's.ID = e.State', 'LEFT');
$this->db->join('employee_image i', 'i.Employee = e.ID', 'LEFT');
$this->db->group_by('e.ID');

Ещё вопросы

Сообщество Overcoder
Наверх
Меню