【问题标题】:How to get the all values from two tables based on phone number如何根据电话号码从两个表中获取所有值
【发布时间】:2017-05-09 15:44:17
【问题描述】:

这里我有两张表,我想从这两张表中得到结果。我尝试了很多,但我无法得到确切的结果,请你帮帮我,我在 codeiniter 中使用这个应用程序。

第一个表

新员工

staff_id              firstName        Mobile          userType

  1                  Soupranjali       9986125566       Teacher
  2                  Sujata            8553880306       Teacher

第二张桌子

新学生

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 格式

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

【问题讨论】:

    标签: php codeigniter model


    【解决方案1】:
    $this->db->join('new_student', 'new_student.fatherMobile  = new_staff.Mobile','left'); 
    

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

    【讨论】:

    • 嗨,我试过你的代码,[ {“staff_id”:“2”,“firstName”:“Sujata”,“userType”:“Teacher”,“student_id”:“1”,“ first_Name”:“janarthan”,“user_type”:“学生”},{“staff_id”:“2”,“firstName”:“Sujata”,“userType”:“老师”,“student_id”:“2”,“ first_Name”:“Santanu”,“user_type”:“Student”},{“staff_id”:“1”,“firstName”:“Soupranjali”,“userType”:“Teacher”,“student_id”:null,“first_Name” :空,“用户类型”:空}]
    • 请检查这个回复这不是一个正确的回复
    • 检查我的预期结果
    • $this->db->join('new_student', 'new_student.fatherMobile = new_staff.Mobile','union');如果在那一行之后没有得到正确答案,请尝试这个,那么可能是其他问题。☺
    【解决方案2】:

    您正在使用 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);
    }
    

    【讨论】:

    • @Bikram Pahi 先生,我尝试了代码,它工作得很好,这里的 staff_id 和 firstName 来了两次不应该这样,请看我的更新答案
    • 员工身份证和名字重复出现
    • 能否请您发布输出?
    • 我尝试了您的第二种解决方案,请在我的帖子中查看我的更新答案,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    相关资源
    最近更新 更多