MySQL SELECT 查看 returns 不同的结果

MySQL SELECT to VIEW returns different results

我整理了一个小的 sql 查询,它从一个 table 中提取数据并将其排序在新的列名下。 sql 看起来像这样:

SELECT course_id AS course, NOW() as datum,
(SELECT COUNT(*) FROM users_courses WHERE course_id = course) AS antal_registrerade,
(SELECT COUNT(*) FROM users_courses WHERE status = 1 AND course_id = course) AS antal_aktiva,
(SELECT COUNT(*) FROM users_courses WHERE status = 3 AND course_id = course) AS antal_avklarade
FROM users_courses GROUP BY course_id

以上查询returns如下:

| course | datum               | antal_registrerade | antal_aktiva  | antal_avklarade   |
-----------------------------------------------------------------------------------------
| 31     | 2016-01-12 16:24:58 | 142                | 19            | 83                |
| 38     | 2016-01-12 16:24:58 | 826                | 45            | 49                |
| 39     | 2016-01-12 16:24:58 | 2                  | 2             | NULL              |
| 43     | 2016-01-12 16:24:58 | 169                | 29            | 32                |
| 44     | 2016-01-12 16:24:58 | 11                 | 4             | 2                 |
| 45     | 2016-01-12 16:24:58 | 67                 | 8             | 7                 |
| 46     | 2016-01-12 16:24:58 | 2                  | 1             | 1                 |   

还好吧?就像我想要的那样。但是当我将此查询另存为视图并且 运行 结果不同时。我得到的每一行的数据都相同,但课程和数据列除外。

| course | datum               | antal_registrerade | antal_aktiva  | antal_avklarade   |
-----------------------------------------------------------------------------------------
| 31     | 2016-01-12 16:24:58 | 1219               | 108           | 174               |
| 38     | 2016-01-12 16:24:58 | 1219               | 108           | 174               |
| 39     | 2016-01-12 16:24:58 | 1219               | 108           | 174               |
| 43     | 2016-01-12 16:24:58 | 1219               | 108           | 174               |
| 44     | 2016-01-12 16:24:58 | 1219               | 108           | 174               |
| 45     | 2016-01-12 16:24:58 | 1219               | 108           | 174               |
| 46     | 2016-01-12 16:24:58 | 1219               | 108           | 174               |   

有人知道这是为什么吗?在已保存视图中找到的 sql 如下所示:

SELECT `database`.`users_courses`.`course_id` AS `course`,now() AS `datum`,
(SELECT COUNT(0) from `database`.`users_courses` where (`database`.`users_courses`.`course_id` = `database`.`users_courses`.`course_id`)) AS `antal_registrerade`,
(SELECT COUNT(0) from `database`.`users_courses` where ((`database`.`users_courses`.`status` = 1) and (`database`.`users_courses`.`course_id` = `database`.`users_courses`.`course_id`))) AS `antal_aktiva`,
(SELECT COUNT(0) from `database`.`users_courses` where ((`database`.`users_courses`.`status` = 3) and (`database`.`users_courses`.`course_id` = `database`.`users_courses`.`course_id`))) AS `antal_avklarade`
FROM `database`.`users_courses`
GROUP BY `database`.`users_courses`.`course_id`

使用条件聚合来表达要简单得多:

SELECT course_id AS course, NOW() as datum,
       COUNT(*) as antal_registrerade,
       SUM(status = 1) as antal_aktiva,
       SUM(status = 3) AS antal_avklarade
FROM users_courses
GROUP BY course_id;

这应该可以解决您的结果问题。

由于某种原因,保存的视图代码的关联子句不正确。我的猜测是您在 table 中没有 coursecourse_id 的两列,因此您的第一个查询不完全是进入视图的内容。无论如何,请使用更简单的查询来解决此问题。