2016-09-22 58 views
0

我有一个查询加入3个表。使用mysql加入显示不符值

$this->db->select("a.user_id as id, a.plate_number as plate_number, a.current_lat as lat, a.current_lon as lon, a.created_on as created_on, a.updated_on as updated_on, a.available as available, a.location_id as location_id, b.user_name as name, b.user_email as email, b.user_phone as phone, c.name as location_name"); 
     $this->db->from('user_driver as a'); 
     $this->db->join('user as b', 'a.user_id = b.user_id'); 
     $this->db->join('vendor_location as c', 'a.location_id = c.location_id'); 
     $query = $this->db->get(); 
     $data['driver'] = $query->result_array(); 

user_driver表结构: user_driver

vendor_location表结构: vendor_location

结果: result

我想表明user_driver表的其余部分,即使有不匹配值为vendor_location表。 LocationName字段可填写NULL,而不是根本不显示任何内容。

我试过left outerfull outer但它不起作用。它给我留下了唯一一行显示。

+0

哪个是vendor_location表中的外键? –

+0

以这种方式处理:'... user_driver INNER JOIN用户ON ....左加入vendor_location ON。 ..' – 1000111

+0

'location_id' @ AT-2016 – may

回答

0

具有u试过这样

$this->db->join('user as b', 'a.user_id = b.user_id', 'LEFT'); 
    $this->db->join('vendor_location as c', 'a.location_id = c.location_id', 'LEFT'); 
+0

谢谢,我应该在两个连接查询中都加上'left' :) – may

1

我想表明user_driver表的其余部分,即使有不匹配的价值vendor_location表。

为此,您应该使用LEFT JOIN。它允许您从左侧的表中获取行,即使右表中没有匹配时也是如此。

$this->db->select("a.user_id as id, a.plate_number as plate_number, a.current_lat as lat, a.current_lon as lon, a.created_on as created_on, a.updated_on as updated_on, a.available as available, a.location_id as location_id, b.user_name as name, b.user_email as email, b.user_phone as phone, c.name as location_name"); 
$this->db->from('user_driver as a'); 
$this->db->join('user as b', 'a.user_id = b.user_id'); 
$this->db->join('vendor_location as c', 'a.location_id = c.location_id', 'left');//modify this line 
$query = $this->db->get(); 
$data['driver'] = $query->result_array(); 
+1

不错的答案。 thx为xampp链接btw – Drew

+0

我只需要在'join'查询中添加'left'。谢谢btw :) – may