如何根据codeigniter中的手机号码从两个表中获取结果

时间:2017-05-09 12:00:00

标签: php codeigniter model

这里我有两个表,我想根据手机号码从这两个表中选择一个选择结果。

第一张表

  

new_staff

staff_id              firstName        Mobile          userType

  1                  Soupranjali       9986125566       Teacher
  2                  Sujata            8553880306       Teacher

第二张表

  

new_student

student_id           first_Name        fatherMobile        user_type

  1                  janarthan         8553880306         Student
  2                  Santanu           8277904354         Student

这里 8553880306 这两个表都可用所以我需要这样的输出

  

预期结果

    {
  "status": "Success",
  "Profile": [
    {
      "staff_id": "2",
      "firstName": "Sujata",
      "userType" : "Teacher"
    },
    {
      "student_id": "1",
      "firstName": "janarthan",
      "user_type" : "Student"
    },
  ]
}

所以尝试了这样,但我无法得到答案,所以请任何人帮助我,

  

我的模特

 public function android_memberList($mobile)
            {
                $this->db->select('new_staff.staff_id, new_staff.firstName, new_staff.userType, new_student.student_id, new_student.first_Name, new_student.user_type');
                //$this->db->select('*');
                $this->db->from('new_staff');
                $this->db->join('new_student', 'new_student.fatherMobile  = new_staff.Mobile');
                $query = $this->db->get();

                # result_array is used to convert the data into an array
                $result = $query->result_array(); // or  $result = $query->result_array()[0]; 
                echo json_encode($query);
            }

更新了答案

     [
  {
    "staff_id": "2",
    "firstName": "Sujata",
    "userType": "Teacher",
    "student_id": "1",
    "first_Name": "janarthan",
    "user_type": "Student"
  }
]
  

更新答案2

    [
  [
    {
      "staff_id": "2",
      "firstName": "Sujata",
      "userType": "Teacher",
      "student_id": "1",
      "first_Name": "janarthan",
      "user_type": "Student"
    }
  ]
]

2 个答案:

答案 0 :(得分:1)

您需要在两个表上检查移动列的数据类型。

它们必须具有相同的数据类型才能执行连接操作。

答案 1 :(得分:1)

你可以试试这个

public function android_memberList($mobile)
{
    $this->db->select('new_staff.staff_id, new_staff.firstName, new_staff.userType, new_student.student_id, new_student.firstName, new_student.user_type');
    //$this->db->select('*');
    $this->db->from('new_staff');
    $this->db->join('new_student', 'new_student.fatherMobile  = new_staff.Mobile');
    $query = $this->db->get();

    # result_array is used to convert the data into an array
    $result = $query->result_array(); // or  $result = $query->result_array()[0]; 
    echo json_encode($result);
}
  

使用result_array时,它将作为ZERO索引数组。您可以选择,也可以使用以上代码

上的评论将其删除