PHP,Mysql子查询用where子句

时间:2018-02-07 00:04:58

标签: php mysql sql

我有一个显示所有CLASS详细信息的查询,现在我想要一个子查询来计算该类中所有已注册的学生。

我尝试使用两个单独的查询并尝试合并它们但我没有成功这样做。

这是我目前的查询:

$str = "SELECT
                class.id,
                class.code AS classcode,
                section.name AS sectionname,
                subject.code,
                class.units,
                sched.name AS schedule,
                class.slots,
                class.dissolved,
                (SELECT
                    Count(enrolldet.enrollno)
                    FROM
                    enrolldet
                    Inner Join enroll ON enrolldet.enrollno = enroll.enrollno
                    Inner Join class ON enrolldet.class = class.id
                    WHERE
                    enroll.validated =  '1' AND
                    class.id =  class.id) AS enrolled
            FROM
                class 
                Left Join sched ON class.sched = sched.id
                Left Join section ON class.section = section.id
                Left Join subject ON class.subject = subject.id
            ";

3 个答案:

答案 0 :(得分:0)

 $str = "SELECT
                c.id,
                c.code AS classcode,
                section.name AS sectionname,
                subject.code,
                c.units,
                sched.name AS schedule,
                c.slots,
                c.dissolved,
                (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 enrolled
            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";

我用这个得到了正确的答案。备查。我刚刚为每个表添加了别名

答案 1 :(得分:0)

您不能拥有主查询和名为class的子查询的class表,因为子查询可以访问这两个表,这会导致SQL由于含糊不清,引擎感到困惑。

要修复您的查询,请为内部提供别名,例如cls,并将AND子句更改为:

AND `class`.`id` = `cls`.`id`

另外,请注意,通过完全删除内部class表可以简化子查询,因为子查询可以访问主查询的class表,如上所述。

<强>代码:

SELECT
   `class`.`id`,
   `class`.`code` AS `classcode`,
   `section`.`name` AS `sectionname`,
   `subject`.`code`,
   `class`.`units`,
   `sched`.`name` AS `schedule`,
   `class`.`slots`,
   `class`.`dissolved`,
   (
      SELECT COUNT(`enrollno`)
      FROM `enrolldet`
      INNER JOIN `enroll`
      ON `enrolldet`.`enrollno` = `enroll`.`enrollno`
      WHERE `enroll`.`validated` = '1'
      AND `enrolldet`.`class` = `class`.`id`
   ) AS `enrolled`
FROM `class`
LEFT JOIN `sched` ON `class`.`sched` = `sched`.`id`
LEFT JOIN `section` ON `class`.`section` = `section`.`id`
LEFT JOIN `subject` ON `class`.`subject` = `subject`.`id`

答案 2 :(得分:0)

您不需要子查询中的所有连接。这应该足够了:

           (SELECT Count(*)
            FROM enrolldet ed JOIN
                 enroll e
                 ON ed.enrollno = e.enrollno
            WHERE e.validated =  1 AND
                  ed.class = c.id
           ) AS enrolled

也就是说,您在子查询中不需要class。并且,我猜测validated是一个数字,所以我删除了#34; 1&#34;周围的单引号。