什么是通过Codeigniter中的三个不同表获取数据的子查询?

时间:2014-12-01 09:42:31

标签: php mysql codeigniter

我在传递 User_name 时参考了我必须获取第三个表中的 visit_id ,因为我的方法有点乱,任何人都可以告诉如何使用Codeigniter子查询实现这一目标?

  public function fetch_patient_bill($user_name) {

    $this->db->select('id');
    $this->db->from('user');
    $query = $this->db->where('user_name', $user_name);
    $result = $query->get();
    foreach ($result->result() as $user) {}
    $user_id = $user->id;

    $this->db->select('id');
    $this->db->from('patient_registration');
    $query = $this->db->where('user_id', $user_id);
    $result = $query->get();
    foreach ($result->result() as $registration) {}
    $registration_id = $registration->id;

    $this->db->select('id');
    $this->db->from('patient_visit');
    $query = $this->db->where('patient_id', $registration_id);
    $result = $query->get();
    foreach ($result->result() as $visit) {}
    $visit_id = $visit->id;   //i want to fetch this id
    }

1 个答案:

答案 0 :(得分:0)

我不清楚您的问题,但您可以在此链接后面的CI中查看使用子查询的示例:How to use subqueries in active record with Codeigniter

StackO上也有类似的帖子:subquery in codeigniter active record

希望这有帮助