如何使用特定条件在codeigniter中加入列

时间:2017-04-25 08:50:21

标签: php mysql codeigniter join

我有2张桌子。

  • 管理员
  • 简档

admins表中,有admin_id,我想加入profile,其中包含字段identifier。让我们假设admin_id =1000000。我想像这样加入它。

$this->db->select('*')->from('admins')->join('profile', 'admin.id = "ABC".admins.admin_id."U"')->where('email', $username)->where('password', $password)->where('status', 1);

admin_id是1000000 标识符是ABC1000000U

如何根据这些表加入这两个表?

2 个答案:

答案 0 :(得分:2)

试试这个:

$this->db
  ->select('*')
  ->from('admins')
  ->join('profile', 'CONCAT("ABC", admins.admin_id, "U") = profile.identifier')
  ->where('email', $username)
  ->where('password', $password)
  ->where('status', 1);

答案 1 :(得分:1)

如果您使用的是JOIN,则两列必须具有相同的datatype,并且它们之间应该有参考,根据您的要求,您可以在CI代码下面尝试,

// create where condition array
$where = array('email'=>$username,'password'=> $password,'status'=>1);
// if there is admin_id then add more conditions in it
if(isset($admin_id) && $admin_id){
    $where['a.admin_id']=$admin_id; // from admins table
    $where['p.identifier']='ABC'.$admin_id.'U'; // from profiles table
}
$this->db->select('*')
     ->from('admins a,profile p') // join not required
     ->where($where); // add where at a glance