如何使用Codeigniter连接多列

时间:2018-09-22 09:37:06

标签: php mysql codeigniter

我需要一个帮助。我需要在codeigniter中使用多列联接两个表。我正在解释下面的表格。

  

pt_car_locations

id     pickup_location_id       dropoff_location_id     price

   1           21                           22                100

   2           23                           24                200
  

pt_locations

 id              location

  21              Cappa

  22              United

  23              Mascut

  24              ABCD     

在这里,我需要通过加入两个表来检索上落地点。我在下面解释查询。

$this->db->select('cl.pickup_location_id,cl.dropoff_location_id,cl.price,l.id,l.location');
$this->db->from('pt_car_locations as cl');
$this->db->join('pt_locations as l', 'cl.pickup_location_id = l.id', 'inner');

在这里,我需要同时加入两列并获取每行中的两个位置。

1 个答案:

答案 0 :(得分:0)

可能需要两次联接,一次联接的表别名为l,一次联接的表别名为l2

$this->db->select('cl.pickup_location_id,cl.dropoff_location_id,cl.price,l.id,l.location');
$this->db->from('pt_car_locations as cl');
$this->db->join('pt_locations as l', 'cl.pickup_location_id = l.id', 'inner');
$this->db->join('pt_locations as l2', 'cl.dropoff_location_id = l2.id', 'inner');