![]() |
Gets too much data - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6) +--- Forum: CodeIgniter 2.x (https://forum.codeigniter.com/forumdisplay.php?fid=18) +--- Thread: Gets too much data (/showthread.php?tid=61551) |
Gets too much data - Toddles - 04-24-2015 Hello all I have this database query but I get all dates back to 2010 and not in the range selected.. SELECT* FROM ( `morning_qa` ) WHERE`qa_date`>='2014-04-24' AND`qa_date`<='2015-04-24' AND`qa_type`='iris' OR`qa_type`='cones' ORDERBY`qa_date`; PHP Code: function morningQA_getall($range_type, $range_num, $qa_year, $qa_type){ Not sure why. It doesn't like the both clause. Works fine for either cones or iris on their own. RE: Gets too much data - CroNiX - 04-24-2015 I think you'd want to use parens around the (and or) so it will use one OR the other, but not both: SELECT* FROM ( `morning_qa` ) WHERE`qa_date`>='2014-04-24' AND`qa_date`<='2015-04-24' AND (`qa_type`='iris' OR `qa_type`='cones') ORDERBY`qa_date`; RE: Gets too much data - Toddles - 04-24-2015 (04-24-2015, 01:13 PM)CroNiX Wrote: I think you'd want to use parens around the (and or) so it will use one OR the other, but not both: PHP Code: if ($qa_type == "iris"){ How do you add the parens. RE: Gets too much data - CroNiX - 04-24-2015 CI2 doesn't have a way to automatically add them and only produces basic queries, not the more complex ones. CI3 is a bit better with that. I just usually use $this->db->query(raw_sql) for more complex queries. but you can also use a string in active record for WHEREs, like: PHP Code: $this->db Would produce: WHERE something = 'any' AND (something_else = cond2 OR something_new = cond3) If cond2 or cond3 are user input, you'd have to db::escape() them manually. |