爲了使這個儘可能簡單明白我會給你所有的細節,我可以讓我可以找出這個問題。我的查詢是重複的結果? MySQL/PHP/Codeigniter
好了,所以我的問題是,我查詢獲取我從我的數據庫ONE記錄返回似乎是在多個陣列相同的記錄的重複值。當我檢查查詢結果時,所有的表都被複制並放入數組中 - 每個重複記錄(或行)都有一個數組。
這個我覺得原因是我的1 - >很多關係表(程序 - > Procedure_event)
這裏是我的瑪(你會看到程序和procedure_event)是1:M的關係。
Here是我的查詢結果,你可以看到我的表(除了過程事件)重複與每個陣列中的相同的數據。返回數組的數量取決於有多少個procedure_event行與過程鏈接。
在這個列表中有三個! - >http://pastebin.com/pc53Pmgf
爲了說明你這裏發生了什麼是一個圖:
查詢結果:
程序行X ---> procedure_event行X
程序x行---> procedure_event row x + 1
程序行x ---> procedure_event row x + 1
我想要什麼返回是這樣的(其他表的不重複):
程序行X ---> procedure_event 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循環。
在公主Leiah的話
「幫幫我Obi'wan,你我唯一的希望!「
謝謝!