CodeIgniter Forums
how to do this query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: how to do this query (/showthread.php?tid=31410)



how to do this query - El Forum - 06-17-2010

[eluser]gebe[/eluser]
how to do this query with active record class?

WHERE
(field = 'negocio' OR field IS NULL)
AND
other_field = 1

Thanks


how to do this query - El Forum - 06-17-2010

[eluser]Clooner[/eluser]
What did you try yourself?

How AR works is really well documented: http://ellislab.com/codeigniter/user-guide/database/active_record.html#select.


how to do this query - El Forum - 06-17-2010

[eluser]siubie[/eluser]
hai Smile interesting, dunno this is my bad on my code @_@

if i make this AR

Code:
$this->db->where('field1',$var1);
$this->db->or_where('name',$name);
$this->db->where('gotcha',$var2);

result in query

Code:
where field1=john and name=doe and gotcha=hehe

maybe this question point to how we make the query to

Code:
where ( field1=john or name=doe ) and gotcha=hehe



how to do this query - El Forum - 06-17-2010

[eluser]danmontgomery[/eluser]
AR doesnt' support grouped where statements, you woulud need to do it manually.

Code:
$this->db->where('(`field1` = "'.$var1.'" OR `field2` IS NULL)', null, false)->where('field3', 'some value');



how to do this query - El Forum - 06-17-2010

[eluser]gebe[/eluser]
Hi Clonner, I already read the document, but is not enought for do my query.
I try with or_where, where_in, etc, etc

Exactly siubie I need the query:

WHERE (field=x OR field IS NULL) AND field_2 = b


how to do this query - El Forum - 06-17-2010

[eluser]gebe[/eluser]
OK nocturn, Thanks


how to do this query - El Forum - 06-17-2010

[eluser]Clooner[/eluser]
[quote author="gebe" date="1276829361"]Hi Clonner, I already read the document, but is not enought for do my query.
I try with or_where, where_in, etc, etc

Exactly siubie I need the query:

WHERE (field=x OR field IS NULL) AND field_2 = b[/quote]

From the manual
Code:
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);

change it into
Code:
$where = "(name='Joe' OR status='active') AND status='boss' ";
$this->db->where($where);