【发布时间】:2011-10-18 03:30:57
【问题描述】:
为了让这件事尽可能简单易懂,我将尽我所能向您提供所有细节,以便找出这个问题。
好的,所以我的问题是,我从数据库中获取 ONE 记录的查询正在返回多个数组中同一记录的重复值。当我检查我的查询结果时,我的所有表都被复制并放入数组中 - 每个重复的记录(或行)一个数组。
我认为造成这个的原因是我的 1 --> 多关系表(Procedure --> Procedure_event)
这是我的shema(你会看到procedure和procedure_event)是1:M的关系。
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