【问题标题】:My query is duplicating results? MySQL / PHP/ Codeigniter我的查询重复结果? MySQL / PHP / Codeigniter
【发布时间】:2011-10-18 03:30:57
【问题描述】:

为了让这件事尽可能简单易懂,我将尽我所能向您提供所有细节,以便找出这个问题。


好的,所以我的问题是,我从数据库中获取 ONE 记录的查询正在返回多个数组中同一记录的重复值。当我检查我的查询结果时,我的所有表都被复制并放入数组中 - 每个重复的记录(或行)一个数组。

我认为造成这个的原因是我的 1 --> 多关系表(Procedure --> Procedure_event)

这是我的shema(你会看到procedure和procedure_event)是1:M的关系。

http://i.imgur.com/Dju0G.png


Here 是我的查询结果,您可以看到我的表 (除了过程事件) 在每个数组中重复使用相同的数据。返回的数组数量取决于有多少 procedure_event 行链接到 procedure

在这个列表中有三个! --> http://pastebin.com/pc53Pmgf

为了向您说明这里发生的事情是一个图表:

查询结果:

过程行 x ---> 过程_事件行 x

过程行 x ---> 过程_事件行 x+1

过程行 x ---> 过程_事件行 x+1

我要返回的是这个(其他表不重复):

程序行 x ---> 程序事件 x

           ---> procedure_event x+1

            ---> procedure_event x+1

这是我在 Codeigniter Active Record 中的查询

public function view_record($record_id)
        {
            //The criteria used for WHERE is a variable with array of procedure_id = record_id that is passed to function
            //e.g. Record id = 2419
            $criteria = array 
            (
                'procedure_id' => $record_id
            );

            //this selects the columns from procedure table for use in joins 
            $this->db->select('*');
            //this is the table I want to use the columns from
            $this->db->from ('procedure');

            //this joins the patient row that matches its ID to the patient ID in the procedure table
            $this->db->join('patient', 'patient.patient_id = procedure.patient_id', 'left');

            //this joins the department row that matches its ID to the patient ID in the procedure table
            $this->db->join('department', 'department.department_id = procedure.department_id', 'left');

            //this joins the procedure_name row that matches its ID to the patient ID in the procedure table
            $this->db->join('procedure_name', 'procedure_name.procedure_name_id = procedure.name_id', 'left');

            //this joins the dosage row that matches its ID to the patient ID in the procedure table]
            $this->db->join('dosage', 'dosage.dosage_id = procedure.dosage_id', 'left');

            //----->This is what is causing the problem -- try a different JOIN?
            $this->db->join('procedure_event', 'procedure.procedure_id = procedure_event.procedure_id' , 'left');

            //this selects the row in procedure table that matches the record number
            $this->db->where('procedure.procedure_id', $record_id);

            //gets all the data from the query
            $result = $this->db->get();


            //
            if ($result->num_rows >0)
            {
                return $result->result();
            }

            else
            {
                return FALSE;

            }


        }

我希望每个人都清楚这一点。我希望有办法解决这个问题,否则我会被卡住,因为我不知道如何处理所有这些重复的数据,因为它弄乱了我的 foreach 循环。

用莉亚公主的话

“帮助我欧比旺,你是我唯一的希望!”

谢谢!

【问题讨论】:

    标签: php mysql codeigniter join


    【解决方案1】:

    那么,如果我理解正确的话,您希望procedure_event 作为嵌套数组返回吗?是这样吗?

    如果是这样,我不确定 Codeigniter 是否提供了直接执行此操作的方法。您最好的选择可能是处理您当前使用 php 获得的数组,将数据放入您想要的嵌套非重复格式。

    您也可以将其分解为 2 个查询。一种是获取所有过程数据,使用所有连接但不使用 procedure_event 连接。然后执行第二个查询,您只需将过程加入到 procedure_event。然后,循环遍历结果并按照您希望的方式准备数据将是一件简单的事情。

    【讨论】:

      【解决方案2】:

      您需要在php中处理查询结果数组,如下所示。

      $result = $this->db->get();
      foreach($result as $key=>$value){
        $result_arr[$value->procedure_id] = $value;
      }
      

      然后

      return $result_arr;
      

      【讨论】:

        猜你喜欢
        • 2019-03-21
        • 2017-09-27
        • 2019-10-21
        • 2015-07-20
        • 2015-08-13
        • 1970-01-01
        • 2011-06-01
        • 2015-11-10
        • 1970-01-01
        相关资源
        最近更新 更多