简体   繁体   English

Codeigniter join返回最后一个id

[英]Codeigniter join gives last id back

I want to have the id from my table 'device_id' but I keep getting the id from 'inventory' because it is the last join. 我希望从我的表'device_id'获取id,但我一直从'inventory'获取id,因为它是最后一个连接。

I have tried to look it up on the internet but could found a solution for Codeigniter. 我试图在互联网上查找,但可以找到Codeigniter的解决方案。

$this->db->where('device_id', $this->input->get('device_id'));

$this->db->join('repair_components', 'repair_options.component_id = repair_components.id');

$this->db->join('products', 'repair_options.product_id = products.id');

$this->db->join('inventory', 'repair_options.product_id = inventory.product_id', 'left');

$query = $this->db->get('repair_options');
$this->data['options'] = $query->result();

print_r($this->data['options']);

I want the id from 'device_id' in my array '$this->data['options']'. 我希望我的数组'$ this-> data ['options']'中的'device_id'中的id。

Give table name to column in where clause and also make sure $this->input->get('device_id') have value in it. 表名赋给where子句中的列,并确保$this->input->get('device_id')具有值。

$this->db->join('repair_components', 'repair_options.component_id = repair_components.id');
$this->db->join('products', 'repair_options.product_id = products.id');
$this->db->join('inventory', 'repair_options.product_id = inventory.product_id', 'left');
$this->db->where('table_name.device_id', $this->input->get('device_id'));
$query = $this->db->get('repair_options');
$this->data['options'] = $query->result();

print_r($this->data['options']);

Simple but tricky one. 简单而棘手的一个。 all you need to do some changes.. 所有你需要做一些改变..

$this->db->select('*,device_id.id as the_device_id');
$this->db->from('repair_options');
$this->db->where('device_id', $this->input->get('device_id'));

$this->db->join('repair_components', 'repair_options.component_id = repair_components.id');

$this->db->join('products', 'repair_options.product_id = products.id');

$this->db->join('inventory', 'repair_options.product_id = inventory.product_id', 'left');

$query = $this->db->get();
$this->data['options'] = $query->result();

print_r($this->data['options']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM