MySql在codeigniter中使用两个不同的join语句加入两个表

时间:2013-05-24 07:50:12

标签: mysql codeigniter

以下连接是否在MySQL中有效

$this->db->select('A.plan_prepared_by,A.approval_date, U.display_name');
$this->db->from('A');
$this->db->where('plan_id', $planid );
$this->db->join('U','U.id=A.approval_user_id','left');
$this->db->join('U','U.username =A.plan_prepared_by','left');

1 个答案:

答案 0 :(得分:1)

您可以通过添加别名

来尝试这样做
$select =   array(
                    'A.plan_prepared_by',
                    'A.approval_date',
                    'UL.display_name'
);
$where  =   array(
                    'plan_id'   =>  $planid
);

$this->db->select($select);
$this->db->from('A');
$this->db->where($where);
$this->db->join('U AS UL','UL.id = A.approval_user_id','left');
$this->db->join('U AS UR','UR.username = A.plan_prepared_by','left');