如何根据移动

时间:2017-05-10 03:04:26

标签: php codeigniter model

这里我有两张桌子,我想从这两张桌子得到结果。我试了很多但是我无法得到确切的结果你能不能帮助我,我在codeiniter中使用这个应用程序。

第一张表

  

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
  3                  Sarvan            8553880306         Student

这里 8553880306 这两个手机号码都存在,所以想要获得两个表格的结果

  

预期结果

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

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

  

我的模特

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

        # result_array is used to convert the data into an array
        $result_new_staff = $query1->result_array(); 

        $this->db->select('new_student.student_id, new_student.first_Name, new_student.user_type');
        $this->db->from('new_staff');
        $this->db->join('new_student', 'new_student.fatherMobile  = new_staff.Mobile');
        $query2 = $this->db->get();

        # result_array is used to convert the data into an array
        $result_new_student = $query2->result_array(); 

        $final_result=array_merge($result_new_staff,$result_new_student);

        $result["status"] = "Success";
        $result["Profile"] = $final_result;
        echo json_encode($result);
    }

根据我的查询,它会像这样返回输出,但这不是我期望的json fomat

  • 这里staff_id和firstName是两次来两次,实际结果只有一次来,任何一个看到我预期的结果并告诉我解决方案
  

我的结果

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

1 个答案:

答案 0 :(得分:0)

  

在选择查询中使用分组依据

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

    # result_array is used to convert the data into an array
    $result_new_staff = $query1->result_array(); 

    $this->db->select('new_student.student_id, new_student.first_Name, new_student.user_type');
    $this->db->from('new_staff');
    $this->db->join('new_student', 'new_student.fatherMobile  = new_staff.Mobile');
$this->db->group_by('new_staff.staff_id');
    $query2 = $this->db->get();

    # result_array is used to convert the data into an array
    $result_new_student = $query2->result_array(); 

    $final_result=array_merge($result_new_staff,$result_new_student);

    $result["status"] = "Success";
    $result["Profile"] = $final_result;
    echo json_encode($result);
}