2011-08-02 22 views
2

これをできるだけシンプルにするために、私はできることすべてを詳細に説明しますので、この問題を理解することができます。私のクエリは結果を複製していますか? MySQL/PHP/Codeigniter


わかりました私の問題は、私のクエリは私のデータベースからONEレコードが複数のアレイ内の同じレコードの重複した値と思われるものを返している私を取得しているということですので。私のクエリ結果をチェックすると、すべてのテーブルが複製され、配列に配置されます。複製されたレコード(または行)ごとに1つの配列です。

私は私の1だと思うこの原因 - >多くの関係テーブル(手順 - > Procedure_event)

ここでは私のシェマです(プロシージャとprocedure_eventが表示されます)1:Mの関係。あなたはは、各アレイ内の同じデータで繰り返される(手順イベントとは別に)私のテーブルを見ることができるよう


Here

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

は、私のクエリの結果です。返される配列の数は、procedure_eventの行がの手順にリンクされているかどうかに依存します。

このリストには3つあります! - >ここで何が起こっているかあなたに説明するためにhttp://pastebin.com/pc53Pmgf

は図である。

クエリ結果:

手順行のx ---> procedure_event行のx

手順行のx ---> procedure_event行x + 1

手順行x --->手続き行x + 1

私が返さたいのは、この(他のテーブルの無重複)である:ここでは

手順行のx ---> procedure_event X

  ---> procedure_event x+1 

      ---> procedure_event x+1 

は私のクエリですin Codeigniterアクティブレコード

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ループを台無しにしてしまいます。プリンセスLeiahの言葉で

「私にObi'wan、あなたの私の唯一の希望を助けて!「

ありがとう!

答えて

1

をので、私が正しくあなたを理解していれば、あなたはprocedure_eventは、ネストされた配列として返すことにしたいですか?この権利ですか?

もしそうなら、私は、もしわかりませんCodeigniterはこれを直接行う方法を提供しています。おそらく、現在PHPを使用している配列を処理して、必要なネストされた非重複フォーマットにデータを置くことでしょう。

また、 2つのクエリに分かれています.1つはすべてのジョインを使用してすべてのプロシージャデータを取得しますが、 procedure_eventジョイントは除外されます。次に、プロシージャをprocedure_eventに結合するだけの2番目のクエリを実行します。結果をループして、好きなようにデータを準備するのは簡単なことです。

0

あなたは次のようにPHPでクエリ結果配列を処理する必要があります。

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

、その後

return $result_arr; 
関連する問題