title: Conditional in MYSQL where clause
body: <p>I have a query that if a flag called OrderBy is 1 then it is a calendar event and I need to check for a range between two date time fields. Whereas all other types we just check for the day you are viewing. I research if statements and saw many posts where it was suggested a case be used so I tried to implement it into my query. My query needs the condition in the where. I know I have syntax issues and that is why I am here in the hopes that someone could point out the correct way to do this.</p>
  
  <p>Thank you for your time</p>
  
  <p>My query currently </p>
  
  <pre><code>SELECT          activities.*, 
                  activitytypes.orderby 
  FROM            activities 
  LEFT OUTER JOIN activitytypes 
  ON              activities.typeid = activitytypes.typeid 
  WHERE           activities.userid = 86 
  AND             activities.typeid NOT IN ( 5, 
                                            10, 
                                            11, 
                                            12, 
                                            19 ) 
  AND 
                  CASE 
                                  WHEN activities.orderby = 1 THEN activities.starttime &gt;= '2013-08-26 04:00:00' 
                                  AND             activities.endtime &lt;= '2013-08-27 04:00:00' 
                                  ELSE activities.activitydate = '2013-08-26' 
                                  order BY        activitytypes.orderby, 
                                                  activities.starttime
  </code></pre>