2016-09-22 3 views
0

が、私はこれをしようとしているところ、と合流します。しかし、もし$guests = 2;私はこの1行の結果があります。 likeor_likeを代わりに使用する場合は、同じです。とor_whereです。私は、この行をコメントした場合:すべてが正常に動作します複数のアクティブレコードのようにしてCodeIgniterの3 or_like

$this->db->or_where('l.city LIKE', $location.'%');

、私は$guests != 3と1行の結果$guests = 3かの場合は結果を持っていません。 $this-db->last_query()

生成されたクエリは次のとおりです。

SELECT 'l'.'id', 'd'.'guests', 'l'.'city', 'l'.'country' FROM 'offers' as 'o' LEFT JOIN 'location' as 'l' ON 'l'.'id' = 'o'.'id' LEFT JOIN 'desc' as 'd' ON 'd'.'id' = 'o'.'id' WHERE 'd'.'guests' = 3 AND 'o'.'completed' = 1 AND 'l'.'country' LIKE 'a%' OR 'l'.'city' LIKE 'a%' LIMIT 5

どのように私はこのクエリを作ることができますか? 完了値= 1、ゲスト数= $guests、都道府県のいずれかが$locationのような値を選択します。

答えて

1

あなたはまたQuery grouping

$location = 'a'; 
$this->db->select('l.id, d.guests, l.city, l.country'); 
$this->db->from('offers as o'); 
$this->db->join('location as l', 'l.id = o.id', 'left'); 
$this->db->join('desc as d', 'd.offer_id = o.id', 'left'); 
$this->db->where('d.guests', $guests); 
$this->db->where('o.completed', 1); 
$this->db->->group_start(); 
$this->db->where('l.country LIKE', $location.'%'); 
$this->db->or_where('l.city LIKE', $location.'%'); 
$this->db->group_end(); 
$this->db->limit(5); 

を使用する必要が

$location = 'a'; 
$this->db->select('l.id, d.guests, l.city, l.country'); 
$this->db->from('offers as o'); 
$this->db->join('location as l', 'l.id = o.id', 'left'); 
$this->db->join('desc as d', 'd.offer_id = o.id', 'left'); 
$this->db->where('d.guests', $guests); 
$this->db->where('o.completed', 1); 
$this->db->where("(l.country LIKE '".$location."%' or l.city LIKE '".$location."%')"); 
$this->db->limit(5); 
+0

'あなたのSQL構文でエラーが発生しています。あなたのMySQLサーバのバージョンに対応するマニュアルをチェックし、 '%または 'l'の近くで使用する正しい構文を確認してください。' 'LIKE a%)LIMIT 5' ' – gdfgdfg

+0

私の答えを更新しました – parth

0

このコードを試してみてください、この'l.country LIKE'のようなコードを避けるためにlike and or_like方法を見てみましょう。

0

試してみてください。この

$location = 'a'; 
$this->db->select('l.id, d.guests, l.city, l.country'); 
$this->db->from('offers as o'); 
$this->db->join('location as l', 'l.id = o.id', 'left'); 
$this->db->join('desc as d', 'd.offer_id = o.id', 'left'); 
$this->db->where('d.guests', $guests); 
$this->db->where('o.completed', 1); 
$this->db->like('l.country',$location,'after'); 
$this->db->or_like('l.city',$location,'after'); 
$this->db->limit(5); 
関連する問題