【问题标题】:How to JOIN a column in codeigniter using a specific condition如何使用特定条件加入 codeigniter 中的列
【发布时间】:2017-09-22 04:24:49
【问题描述】:

我有 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

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

【问题讨论】:

    标签: php mysql codeigniter join


    【解决方案1】:

    试试这个:

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

    【讨论】:

      【解决方案2】:

      如果您使用 JOIN,则两列必须具有相同的 datatype,并且它们之间应该有 REFERENCE,根据您需要的结果,您可以尝试下面的 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
      

      【讨论】:

        猜你喜欢
        • 2020-09-19
        • 2018-08-18
        • 1970-01-01
        • 1970-01-01
        • 2013-04-06
        • 1970-01-01
        • 2017-09-30
        • 1970-01-01
        • 2015-10-02
        相关资源
        最近更新 更多