请帮助我理解正确的连接语法。
我有一个名为inventory的表,其中包含:
trans_id
trans_items items -> item_id
trans_user employees -> person_id
trans_date
trans_comment
trans_inventory
如上所示,trans_items是items表中的外键,trans_user是employees表中的外键,employee id是people表中的外键。
现在我想要做的是在 HTML 中显示库存表,但我希望显示员工姓名,而不是显示员工 ID。
<小时/>编辑...................................................... .. 所以我可以使用以下代码仅显示员工的姓氏:
$this->db->select('inventory.*, items.name ,people.last_name');
$this->db->from('inventory');
$this->db->join('items', 'inventory.trans_items = items.item_id' , 'left');
$this->db->join('people', 'inventory.trans_user = people.person_id' , 'left');
$this->db->where('deleted', 0);
$this->db->order_by('trans_date desc');
带有模型代码:
foreach($report_data as $row)
{
$tabular_data[] = array($row['name'], $row['last_name'],$row['trans_date'], $row['trans_inventory'], $row['trans_comment']);
}
但我需要它是名字和姓氏,所以我做了这些:
$this->db->select('inventory.*, items.name ,CONCAT(people.first_name, " ",people.last_name) as employee');
$this->db->from('inventory');
$this->db->join('items', 'inventory.trans_items = items.item_id' , 'left');
$this->db->join('people', 'inventory.trans_user = people.person_id' , 'left');
$this->db->where('deleted', 0);
$this->db->order_by('trans_date desc');
带有模型代码:
foreach($report_data as $row)
{
$tabular_data[] = array($row['name'], $row['employee'],$row['trans_date'], $row['trans_inventory'], $row['trans_comment']);
}
如果我使用 concat 函数,就会出错。请帮忙。
最佳答案
这应该适合你
$this->db->select('inventory.*, items.name ');
$this->db->from('inventory');
$this->db->join('items', 'inventory.item_id = items.item_id');
$this->db->order_by('trans_date desc');
return $this->db->get()->result_array();
关于php - PHP MySQL 加入帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4612038/