MySQL SELECT到VIEW返回不同的结果

时间:2016-01-12 15:39:35

标签: mysql sql sql-view

我已经整理了一个小的SQL查询,该查询从一个表中提取数据并在新列名下对其进行排序。 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

以上查询返回以下内容:

| 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`

1 个答案:

答案 0 :(得分:2)

使用条件聚合表达这一点要简单得多:

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;

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

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