2016-09-22 10 views
0

3つのテーブルを結合するクエリがあります。mysql 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'); 
     $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 user ON .... LEFT JOIN vendor_location ON。 .. – 1000111

+0

'location_id' @ AT-2016 – may

答えて

0

はuは私がvendor_locationテーブルに値が一致していなくてもuser_driverテーブルの残りの部分を見せたいこの

$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

ようにしようとしています。

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

nice answer。 xamppリンクのthx – Drew

+0

両方の 'join'クエリに' left'を追加するだけです。ありがとうbtw :) – may

関連する問題