如果没有结果,请不要在COUNT()操作期间返回记录

时间:2018-09-08 22:23:14

标签: mysql sql left-join

我正在尝试让多少学生注册一堂课。如果有ARE记录,以上方法适用。

但是,如果没有结果,则返回单个“记录”,其中所有字段均为NULL,而students_count则返回零

+---+------+------+----------------+----------------+
|   | id   | name | professor_name | students_count |
+---+------+------+----------------+----------------+
| 1 | null | null | null           | 0              |
+---+------+------+----------------+----------------+

我希望没有记录返回。

如果一条记录返回,它看起来像这样。

+---+----+-------------+----------------+----------------+
|   | id | name        | professor_name | students_count |
+---+----+-------------+----------------+----------------+
| 1 | 1  | Science 101 | Atkins         | 16             |
+---+----+-------------+----------------+----------------+

我尝试过LEFT JOINIFNULL(COUNT(DISTINCT students.id), null) AS students_count组合的变体

但是我似乎无法使其正常工作。

有帮助吗?

SELECT  classes.*,
        professors.name AS professor_name,
        COUNT(DISTINCT students.id) AS students_count
FROM classes
INNER JOIN professors ON classes.professor_id = professors.id
LEFT JOIN students ON  classes.id = students.class_id AND classes.class_id IS NOT NULL
WHERE classes.class_id = 3

使用下面的通用数据,class_id为3,结果应为无记录。但是返回了计数为0的null记录。

+---+------+------+----------------+----------------+
|   | id   | name | professor_name | students_count |
+---+------+------+----------------+----------------+
| 1 | null | null | null           | 0              |
+---+------+------+----------------+----------------+

使用class_id 1将返回:

+---+----+-------------+----------------+----------------+
|   | id | name        | professor_name | students_count |
+---+----+-------------+----------------+----------------+
| 1 | 1  | Science 101 | Atkins         | 4              |
+---+----+-------------+----------------+----------------+

通用数据

课程

+---+-------------+---------------+
|   | name        | professors_id |
+---+-------------+---------------+
| 1 | Science 101 | 1             |
+---+-------------+---------------+
| 2 | Math        | 2             |
+---+-------------+---------------+
| 3 | English     | 3             |
+---+-------------+---------------+

教授

+----+--------+
| id | name   |
+----+--------+
| 1  | Atkins |
+----+--------+
| 2  | Button |
+----+--------+
| 3  | Castor |
+----+--------+

学生

+----+-------+------------+
| id | name  | classes_id |
+----+-------+------------+
| 1  | Adam  | 1          |
+----+-------+------------+
| 2  | Beth  | 1          |
+----+-------+------------+
| 3  | Chris | 1          |
+----+-------+------------+
| 4  | David | 1          |
+----+-------+------------+
| 5  | Erma  | 2          |
+----+-------+------------+

1 个答案:

答案 0 :(得分:1)

您可以尝试使用INNER JOIN代替OUTER JOIN,因为LEFT JOIN将基于classes表。

SELECT  classes.*,
        professors.name AS professor_name,
        COUNT(DISTINCT students.id) AS students_count
FROM classes
INNER JOIN professors ON classes.professor_id = professors.id
INNER JOIN students ON  classes.id = students.class_id
WHERE classes.class_id = 3

sqlfiddle

编辑

HAVING子句用于集合函数条件。但是您使用的HAVING classes.class_id IS NOT NULL可以移至where

SELECT  classes.*,
        professors.name AS professor_name,
        COUNT(DISTINCT students.id) AS students_count
FROM classes
INNER JOIN professors ON classes.professor_id = professors.id
LEFT JOIN students ON  classes.id = students.class_id 
WHERE classes.class_id = 3 AND classes.class_id IS NOT NULL