使用mysql join显示不匹配的值

时间:2016-09-22 04:14:50

标签: mysql database codeigniter join

我有一个查询要加入3个表。

user_driver

vendor_location表结构: user_driver

user_driver表结构: vendor_location

结果: result

我希望显示vendor_location表的其余部分,即使{for LocationName表没有匹配的值。 NULL字段可以填充left outer,而不是根本不显示任何内容。

我尝试了full outer@Override public long getItemId(int position) { Object obj = getItemAtPosition(position); return obj.hashCode(); } ,但它无效。它让我只有一行显示。

2 个答案:

答案 0 :(得分: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 :(得分:0)

你试过这样的事吗

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