如何根据电话号码从两个表中获取所有值

时间:2017-05-09 15:44:17

标签: 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, 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');
        $query = $this->db->get();

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

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

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

更新回答

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

2 个答案:

答案 0 :(得分:2)

$this->db->join('new_student', 'new_student.fatherMobile  = new_staff.Mobile','left'); 

请在代码中替换此行并检查,我认为它正在运行,并且您可以根据需要获取数据。

答案 1 :(得分:1)

您正在使用MySQL join来合并来自2个表的数据,这将永远无法获得所需的结果。您可以使用union合并两个表中的数据。在您的情况下,问题是两个表中的字段名称都不同。

解决方案1:

在SQL中使用alias概括列名称,问题出在json数组中,您将获得通用键。

解决方案2:

运行2个不同的查询,在2个不同的数组中获取数据,合并2个数组并获得所需的结果。我在这里实施第二个解决方案。

public function android_memberList($mobile)
{
    $this->db->select('distinct(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('distinctnew_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);
}