2017-02-11 6 views
1

と同じ結果を返さない:私はCodeIgniterの中で、このコードを実行するとphpMyAdminの中に、このクエリは、それはいくつかの結果を返しますが、実行した場合CodeIgniterのは、私は2つの異なるテーブルからデータを選択するクエリ実行しているクエリ

$this->db->select('a.any_id, a.name'); 
$this->db->from('table1 a, table2 b'); 
$this->db->where('a.any_id = b.any_id'); 
$this->db->where('b.other_id = "$myId"'); 

を空の配列を返します。

チップはありますか?これは私を夢中にさせている。

+0

'' 'ます$ this-> DB->を選択し( 'a.any_id、a.name'); $ this-> db-> from( 'table1 a、table2 b'); $ this-> db->ここで( 'a.any_id = b.any_id'、NULL); $ this-> db-> get(); return $ query-> result(); ''(); $ this-> db-> get(); $ this-> db->ここで( 'b.other_id = "$ myId" – Mohammad

+0

いいえ、動作しませんでした。前の例と同じ0の結果が –

+0

であり、この '' $ this-> db-> select( 'a.any_id、a.name'); $ this-> db-> from( 'table1 a、table2 b'); $ this-> db->ここで( 'a.any_id'、 'b.any_id'); $ this-> db->ここで( 'b.other_id'、$ myId); $ query = $ this-> db-> get(); return $ query-> result(); '' ' – Mohammad

答えて

0

次の2つのテーブルを結合しようとすると、あなたのあなたにユーザガイドの上CodeIgniterの$this->db->join()スクロールを下に使用していないので、私は思う(加入参照)https://www.codeigniter.com/user_guide/database/query_builder.html

public function example($myID) { 
    $this->db->select('a.any_id, a.name'); 
    $this->db->from('table1 a', 'LEFT'); 
    // $this->db->from('table1 a'); 
    $this->db->join('table2 b', 'b.any_id = a.any_id', 'LEFT'); 
    $this->db->where('a.any_id', 'b.any_id'); 
    $this->db->where('b.other_id', $myId); 
    $query = $this->db->get(); 

    return $query->result(); 
} 

そして、それをvardump。

+0

複数のテーブルを配置するFROMでうまくいけば、CROSS JOINと呼ばれ、WHERE節に 'tablea = tableb'節を追加すると、効果的にINNER JOINに変換されます。 –

0

皆さん、ありがとうございました。

これはjoin節の使用とは関係ありません。

私はちょうど

$this->db->where('b.other_id', $myId); 

$this->db->where('b.other_id = "$myId"'); 

を変更し、完全に働きました。まだ最初の行が単純なクエリに最適であるように、なぜ、なぜか分かりません。

+0

最初の行は最初の行を完全なものとして使用しています.2番目の行は2番目の行が 'この列' =この制約を分割しています。最初は '$ this-> db-> where( 'b.other_id ='。$ myId);'のように書くべきですが、どちらも有効です。 –

0

してみてください。この

 $this->db->select('a.any_id.*,table2.name'); 
     $this->db->from('table1 a'); 
     $this->db->join('tabl2 b', 'a.id=b.id', 'left'); 
     $this->db->where('a.id',$id);  
     $query = $this->db->get(); 
     return $query->result(); 
関連する問題