php - 设计SQL语句: How to JOIN on same table

标签 php mysql sql

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 的值,现在我想加入自己,并想从员工表中获取名字和姓氏,任何人都可以帮助我,我该怎么做?

最佳答案

你可以做类似的事情

$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');

关于php - 设计SQL语句: How to JOIN on same table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47773311/

相关文章:

php - 检查表中所有字段是否为空

mysql - 将 SQL 翻译成 JPQL(SELECT NEW 和 "AS"关键字)

sql - PostgreSQL - 使用数组排序。我可以动态创建数组吗?

mysql - 在 SQL 中添加括号会产生不同的结果

sql - Oracle 的 CURRENT_TIMESTAMP 函数真的是一个函数吗?

php - 以百分比显示完整空间允许的库

php - 将 json 数据解析为变量并在每次迭代时提交给函数

php - if isset语句与javascript?

MySQL 根错误

即使给定值不同,MySQL 也能找到匹配项