我正在尝试在Codeigniter中进行查询,以允许用户将他们过去提交的记录检索到我的数据库。
这是我的架构:
我无法弄清楚我需要为某些表格做些什么
例如1:M或M:M
我认为我需要使用Select,Joins for this并且我已经完成了一些但是对于Many to Many关系和链接表我很困惑如何去做。
这是我到目前为止在代码方面所得到的,但它没有返回任何行,所以我认为这是错误的:
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
$criteria = array
(
'procedure_id' => $record_id
);
//this selects the columns from procedure table for use in joins
$this->db->select('procedure.procedure_id, procedure.patient_id, procedure.department_id, procedure.name_id , procedure.dosage_id');
//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', 'inner');
//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', 'inner');
//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', 'inner');
//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', 'inner');
//this selects the row in procedure table that matches the record number
$this->db->where('procedure_id', $record_id);
/*
Code for other tables:
I need help with
procedure_event, staff, resources, hr
*/
//this part I think is wrong
$result = $this->db->get();
//
if ($result->num_rows >0)
{
echo "There is Data!";
}
else
{
echo "No Data!";
}
}
我收到一条消息说“没有记录”但是我的表格中有数据等:
所以到目前为止我的查询一定是错的。
谢谢你的时间!
答案 0 :(得分:1)
首先,我个人认为在纯文本中编写复杂查询更容易,而不是使用Active Records。
你所有的联盟都使用内心;也就是说所有行都需要在另一个表中找到一个伙伴。如果其中任何一个不存在,您将获得0行。考虑使用LEFT JOIN,如前所述。
目前所有这些表都包含数据吗?
在这些表的每个表中是否确实需要一个伙伴行,或者您是否依赖行丢失的情况?