Codeigniter基于两个连接条件连接两个表

时间:2016-07-15 06:09:16

标签: php mysql sql-server codeigniter join

我正在使用代码点火器实现连接查询。我有两张桌子

 1) Users -> Contains all users
 2) Query -> Contains queries assigned to users. Each query has two users to attend that query.

在Queries表中我有两列

 1) attendingMD -> First user attending that query
 2) secondAttendingMD -> Second user that query.

我想显示查询列表以及参与该查询的两个用户的名称。我设法通过使用此代码获得第一个用户的名称。

  $this->db->select("Query.*, Users.fullname as firstMD");
  $this->db->join('Users', 'Users.id = Query.attendingMD');
  $this->db->where('Query.isCompleted', 1);
  $query = $this->db->get('Query');
  return $query->result_array(); 

请建议我如何实现这一目标。 这是我想要显示表格的表格视图。enter image description here

1 个答案:

答案 0 :(得分:5)

尝试:

$this->db->select("Query.*, uf.fullname as firstMD, us.fullname as secondMD");
$this->db->join('Users uf', 'uf.id = Query.attendingMD');
$this->db->join('Users us', 'us.id = Query.secondAttendingMD');
$this->db->where('Query.isCompleted', 1);
$query = $this->db->get('Query');
return $query->result_array();