title: Mysql query where condition with or in php
body: <p>Hi all i am trying to fetch duedate for particular course when i assigned a course so i have 3 types of table where i can assign course to user in those 3 tables i have userid column same so in my course table it has to satisfy the condition but its not working nw if i gave only one condition is working </p>
  
  <p>Here is my query:</p>
  
  <pre><code> $sql1="SELECT cm.id as cmid,c.id as cid,c.fullname,c.startdate,c.timecreated,c.theme,cm.instance,asg.duedate,asd.duedate,sca.duedate FROM mdl_course c LEFT JOIN mdl_course_modules cm ON c.id=cm.course LEFT JOIN mdl_ppc_assigncourses_group asg On asg.courseid=c.id LEFT JOIN mdl_ppc_assigncourses_dep asd On asd.courseid=c.id LEFT JOIN mdl_ppc_singlecourse_assign sca On sca.courseid=c.id where c.category != 0 and requested='0' AND cm.id!='' AND asg.userid='2101'  and sca.userid= '2101' and asd.userid= '2101' GROUP BY cm.id HAVING COUNT(*) &gt; 0";
  </code></pre>
  
  <p>In that query if i keep only one condition  like this <code>where c.category != 0 and requested='0' AND cm.id!='' AND asg.userid='2101'</code>it is fetching one course i have course form asg.userid also so if i add it its not working </p>
  
  <p>And i am trying to get the duedates for that particular course but for first course i am getting due date as correct for second one duedate is showing as default date(1970-01-01)</p>
  
  <p>Here ia my code:</p>
  
  <pre><code> &lt;?php
              require_once($CFG-&gt;dirroot . '/mod/scorm/lib.php');
              require_once($CFG-&gt;dirroot . '/mod/scorm/locallib.php');
              require_once($CFG-&gt;dirroot . '/course/lib.php');
              global $DB;
              $id = $USER-&gt;id;
  
              $stmt = $DB-&gt;get_records_sql("
                           SELECT asg.asgid,asg.courseid,asg.groupid,asg.duedate FROM mdl_ppc_assigncourses_group asg 
                           JOIN mdl_ppc_group_users pgu ON asg.groupid = pgu.group_id
                           WHERE pgu.userid = '$id' AND asg.status='1'and  pgu.status='1'
                            UNION  SELECT asd.acdid,asd.courseid,asd.depid,asd.duedate FROM mdl_ppc_assigncourses_dep asd 
                           JOIN mdl_ppc_dep_user pdu ON asd.depid = pdu.department 
                           WHERE pdu.userid = '$id' AND asd.status='1' and pdu.status='1'
                           UNION SELECT mpsa.scid,mpsa.courseid,mpsa.depid,mpsa.duedate FROM mdl_ppc_singlecourse_assign mpsa LEFT JOIN mdl_course c ON mpsa.courseid=c.id where mpsa.userid = '$id' and mpsa.status='1'");
  
  
              $cids = '';
              foreach ($stmt as $value) {
                  $cids[] = $value-&gt;courseid;
  
              }
              //print_r($cids);
  
             $sql1="SELECT cm.id as cmid,c.id as cid,c.fullname,c.startdate ,c.timecreated ,c.theme ,cm.instance,asg.duedate ,asd.duedate ,sca.duedate 
      FROM mdl_course c 
      LEFT JOIN mdl_course_modules cm ON c.id=cm.course  AND cm.id!=''
      LEFT JOIN mdl_ppc_assigncourses_group asg On asg.courseid=c.id  AND asg.userid='2101'
      LEFT JOIN mdl_ppc_assigncourses_dep asd On asd.courseid=c.id and asd.userid= '2101' 
      LEFT JOIN mdl_ppc_singlecourse_assign sca On sca.courseid=c.id  and sca.userid= '2101'
      where c.category != 0 and requested='0' 
      GROUP BY cm.id 
      HAVING COUNT(*) &gt; 0";
              if ($cids != '') {
                  $cids = implode(',', $cids);
                  $sql1 .= " and cid in ($cids)";
              }
  
              $courses = $DB-&gt;get_records_sql($sql1);
  
              if (sizeof($courses) &amp;&amp; $cids != '') {
                  foreach ($courses as $row) {
                            echo $row-&gt;duedate;
  
                  }
              }
  </code></pre>
  
  <p>Can anyone help me how can i solve it</p>
  
  <p>Thanks in advance.</p>