php mysql在同一查询中使用子查询结果WHERE子句

时间:2018-06-13 06:05:04

标签: php mysql sql

是否可以在同一查询的WHERE子句中使用子查询结果作为条件。我有这个SQL代码。我想比较ASSESSEDINCLASS,它是子查询中的结果和C.SLOTS,以确定它是否已满。这在单个查询中是否可能?提前谢谢

        $str = "SELECT
                c.id,
                c.code AS classcode,
                section.name AS sectionname,
                subject.code,
                subject.subdesc,
                c.units,
                sched.name AS schedule,
                c.slots,
                c.dissolved,
                c.tutorial,
                c.inst,
                instructor.lname,
                instructor.fname,
                instructor.mname,
                instructor.suffix,
                (SELECT
                    Count(e.enrollno)
                    FROM
                    enrolldet AS e
                    Inner Join enroll ON e.enrollno = enroll.enrollno
                    Inner Join class ON e.class = class.id
                    WHERE
                    enroll.validated =  '1' AND
                    class.id =  c.id) as validatedinclass,
                (SELECT
                    Count(e.enrollno)
                    FROM
                    enrolldet AS e
                    Inner Join enroll ON e.enrollno = enroll.enrollno
                    Inner Join class ON e.class = class.id
                    WHERE
                    enroll.assessed = '1' AND
                    class.id =  c.id) as assessedinclass,
                (SELECT
                    q.id
                    FROM
                        merged
                        Inner Join class AS q ON merged.mothercode = q.id
                    WHERE
                        merged.mergefrom = c.id) AS mergedto_mothercode
            FROM
                class AS c
                Left Join sched ON c.sched = sched.id
                Left Join section ON c.section = section.id
                Left Join subject ON c.subject = subject.id
                Left Join instructor ON c.inst = instructor.userid
                Left Join course ON section.course = course.id
                Inner Join period ON c.period = period.id
            WHERE
                (period.id =  '".$period."' OR period.code = '".$period."')";
    if($status == 'open'){
        $str .= " AND c.slots < assessedinclass";
    }
    $str .= " ORDER BY subject.subdesc";

1 个答案:

答案 0 :(得分:1)

回答你的问题是的,你可以使用Having子句,让你在where子句之后使用它

having assessedinclass = someval

$str .= " HAVING c.slots < assessedinclass";

但我更喜欢不使用依赖子查询而是使用join作为主查询的sub子句,如下所示

SELECT ...., 
       COALESCE(aic.assessedinclass, 0) AS assessedinclass, 
FROM   class AS c 
       JOINS....
       LEFT JOIN (SELECT class.id, 
                         COUNT(e.enrollno) AS assessedinclass 
                  FROM   enrolldet AS e 
                         INNER JOIN enroll 
                                 ON e.enrollno = enroll.enrollno 
                         INNER JOIN class 
                                 ON e.class = class.id 
                  WHERE  enroll.assessed = '1' 
                  GROUP  BY class.id) aic 
              ON aic.id = c.id 
WHERE  ( period.id = '".$period."' OR period.code = '".$period."' ) 
AND c.slots < COALESCE(aic.assessedinclass, 0)