Welcome Guest, Not a member yet? Register   Sign In
How to write this query in Active Record?
#1

[eluser]Unknown[/eluser]
I must write this query for search result

SELECT id FROM ref WHERE cat_id='1' AND (desc_short LIKE '%xxx%' OR desc_long LIKE '%xxx%')

I try with
$this->db->where('ref.cat_id', 1);
$this->db->like('ref.desc_short', 'xxx');
$this->db->or_like('ref.desc_long', 'xxx');

but produces wrong code
SELECT id FROM ref WHERE cat_id='1' AND desc_short LIKE '%xxx%' OR desc_long LIKE '%xxx%'

Anyone can help me?
#2

[eluser]memVN[/eluser]
Code:
$this->db->where('cat_id', 1);
$this->db->like('desc_short', 'xxx');
$this->db->or_like('desc_long', 'xxx');
$q = $this->db->get('ref');
return $q;
#3

[eluser]Unknown[/eluser]
Thank you, but the result is the same.
I need the key parentheses ( )
Otherwise the query returns me just "xxx" in desc_long but without cat_id=1
#4

[eluser]danmontgomery[/eluser]
Code:
$this->db->select('id')->where('cat_id', 1)->where('(desc_short LIKE "%xxx%" OR desc_long LIKE "%xxx%")', NULL, FALSE)->get('ref');
#5

[eluser]elambiguo[/eluser]
Sometimes the simple solution is the best solution...




Theme © iAndrew 2016 - Forum software by © MyBB