codeigniter join返回最后一个id

disbfnqx  于 2021-06-16  发布在  Mysql
关注(0)|答案(2)|浏览(282)

我想从我的表'device\u id'中获取id,但我一直从'inventory'中获取id,因为它是最后一个联接。
我曾试图在互联网上查找它,但可以找到一个解决方案的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']);

我需要数组“$this->data['options']”中“device\u id”的id。

mspsb9vt

mspsb9vt1#

为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']);
kq4fsx7k

kq4fsx7k2#

简单但棘手的一个。你只需要做些改变。。

$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']);

相关问题