简体   繁体   English

如何从基于移动设备的两个表中获取结果

[英]How to fetch the results from two tables based on mobile

Here i have two tables, i want get the results from this two tables.i tried lot but i am unable to get the exact results can you please any help me, i am using this application in codeiniter. 在这里我有两个表,我想从这两个表中获取结果。我尝试了很多,但我无法获得确切的结果,请您帮帮我,我正在codeiniter中使用此应用程序。

First Table 第一张桌子

new_staff 新员工

staff_id              firstName        Mobile          userType

  1                  Soupranjali       9986125566       Teacher
  2                  Sujata            8553880306       Teacher

Second Table 第二表

new_student 新生

student_id           first_Name        fatherMobile        user_type

  1                  janarthan         8553880306         Student
  2                  Santanu           8277904354         Student
  3                  Sarvan            8553880306         Student

here 8553880306 both table this mobile number is present, so want get to the both table results 这里8553880306这两个表都存在此手机号,因此要获取两个表的结果

Expected Results 预期成绩

    {
  "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"
    }
  ]
}

So tried like this but i unable to get the answer, so please anyone help me, 如此尝试过,但我无法获得答案,所以请任何人帮助我,

my model 我的模特

  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);
    }

Based on my query it is returning the output like this, but this is not my expected json fomat 根据我的查询,它将返回这样的输出,但这不是我期望的json fomat

  • Here staff_id and firstName is coming two two time,actual result it has to to come one time only,any one see my expected results and tell me solution 这里staff_id和firstName两次来两次,实际结果只需一次来,任何人都可以看到我的预期结果并告诉我解决方案

my result 我的结果

 {
  "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"
    }
  ]
}

Use Group by in select query 在选择查询中使用分组依据

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM