我想使用Codeigniter从我的数据库返回一些数据。该查询是来自所有相关表的联接。 这是图:
模型中的联接查询:
public function combine_all_data_related($hostname){
$this->db->select('*');
$this->db->from('workstations w');
$this->db->join('occupations o', 'w.occupation_id = o.occupation_id', 'left');
$this->db->join('departments d', 'd.department_id = o.department_id', 'left');
$this->db->join('workstation_configuration wc', 'wc.service_tag = w.service_tag', 'left');
$this->db->join('workstation_models wm', 'wm.workstation_model_id = wc.workstation_model_id', 'left');
$this->db->join('workstation_types wt', 'wt.workstation_type_id = wm.workstation_type_id', 'left');
$this->db->join('users u', 'u.occupation_id = o.occupation_id', 'left');
$this->db->join('phone_numbers pn', 'pn.fmid = u.fmid', 'left');
$this->db->join('phones p', 'p.phone_number_id = pn.phone_number_id', 'left');
$this->db->join('phone_brands pb', 'pb.phone_brand_id = p.phone_brand_id', 'left');
$this->db->where("w.hostname = '" . $hostname . "'");
$query = $this->db->get();
return $return = $query->result();
}
在浏览器中,这是var_dumps结果SQL语句和返回的数据。记录不正确的获取日期,最新编辑和状态。
答案 0 :(得分:1)
您进行df1 <- structure(list(id = 1:3, milk = c(TRUE, FALSE, FALSE), cheese = c(TRUE,
FALSE, FALSE), eggs = c(FALSE, TRUE, FALSE), wheat = c(FALSE,
FALSE, TRUE), barley = c(FALSE, TRUE, FALSE), rye = c(FALSE,
FALSE, FALSE)), class = "data.frame", row.names = c(NA, -3L))
df2 <- structure(list(label = c("vegan", "gluten_free"), boolean = c(FALSE,
FALSE), ingredients = c("milk, cheese, eggs", "wheat, rye, barley"
)), class = "data.frame", row.names = c(NA, -2L))
-从所有指定的表中选择所有列。并且在您的表格中,某些字段具有相同的名称(“ acquisition_date”,“ latest_edit”,“ status” –在“电话”和“工作站”中)。因此,您将获得不可预测的结果。也许phpmyadmin和Codeigniter使用不同的驱动程序。无论如何,仅使用SELECT *
是非常不可靠的。
有2种解决方案:
SELECT *
代替$this->db->select('*');
。例如:
$this->db->select('explicitly list all need fields', NULL);
或者至少在请求中具有相同名称的字段中明确指定名称别名:
$this->db->select('w.hostname ..., w.acquisition_date AS ws_acquisition_date ..., o.occupation_id ..., p.acquisition_date AS ph_acquisition_date ...', NULL);
在PHP中,将这些字段称为:
$this->db->select('*, w.acquisition_date AS ws_acquisition_date, p.acquisition_date AS ph_acquisition_date, w.status AS ws_status, w.latest_edit AS ws_latest_edit, p.latest_edit AS ph_latest_edit', NULL);
我推荐第一个解决方案。