简体   繁体   English

MySQL SELECT到VIEW返回不同的结果

[英]MySQL SELECT to VIEW returns different results

I have put together a small sql query which brings data from one table and sorts it under new column names. 我整理了一个小的sql查询,该查询从一个表中获取数据并将其按新的列名进行排序。 The sql looks like this: 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

The above query returns the following: 上面的查询返回以下内容:

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

All good right? 好吧? Just like I wanted it. 就像我想要的一样。 BUT when I save this query as a view and run that the result is different. 但是,当我将此查询另存为视图并运行时,结果是不同的。 I get the same data for every row, except for the course and datum columns. 除课程列和基准列外,每一行都得到相同的数据。

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

Anyone have any idea why this is? 有人知道为什么会这样吗? The sql found in the saved view looks like this: 在已保存的视图中找到的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`

This is much simpler to express using conditional aggregation: 使用条件聚合表达起来要简单得多:

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;

This should fix the problem with your results. 这应该可以解决您的结果问题。

For some reason, the saved code for the view has the correlation clause incorrect. 由于某种原因,为视图保存的代码具有不正确的关联子句。 My guess is that you don't have two columns in the table for course and course_id , so your first query isn't exactly what is going into the view. 我的猜测是您在表中没有coursecourse_id两列,因此您的第一个查询并不完全是视图的内容。 In any case, fix this using a simpler query. 无论如何,请使用更简单的查询解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM