mysql query where condition with或in php

ejk8hzay  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(280)

嗨,当我分配课程时,我正在尝试获取特定课程的duedate,所以我有3种类型的表,我可以将课程分配给用户在这3个表中,我有相同的userid列,所以在我的课程表中,它必须满足条件,但它不起作用nw如果我只给出一个条件是起作用
我的问题是:

$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(*) > 0";

如果我只保留一个这样的条件 where c.category != 0 and requested='0' AND cm.id!='' AND asg.userid='2101' 它正在获取一个课程,我有课程表asg.userid,所以如果我添加它就不起作用了
我正在尝试获取该特定课程的截止日期,但是对于第一个课程,我得到的截止日期是正确的,对于第二个课程,截止日期显示为默认日期(1970-01-01)
这是我的密码:

<?php
            require_once($CFG->dirroot . '/mod/scorm/lib.php');
            require_once($CFG->dirroot . '/mod/scorm/locallib.php');
            require_once($CFG->dirroot . '/course/lib.php');
            global $DB;
            $id = $USER->id;

            $stmt = $DB->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->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(*) > 0";
            if ($cids != '') {
                $cids = implode(',', $cids);
                $sql1 .= " and cid in ($cids)";
            }

            $courses = $DB->get_records_sql($sql1);

            if (sizeof($courses) && $cids != '') {
                foreach ($courses as $row) {
                          echo $row->duedate;

                }
            }

有人能帮我解决吗
提前谢谢。

hrysbysz

hrysbysz1#

如果对筛选器左联接表列使用where子句,则此操作将作为内部联接。。
您应该在相关的左join on子句中移动这些条件

$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(*) > 0";

相关问题