2012-03-12 60 views
1

我有一个输出到输出MySQL的循环选择

+------------+---------------+ 
| WeekNumber | Count   | 
+------------+---------------+ 
|   5 |    1 | 
|   8 |    2 | 
|   34 |    0 | 
+------------+---------------+ 

SELECT week, count 
FROM mytable; 

如何可以修改查询,这样我可以提供一个周范围(2 - 35)一个MySQL命令和具有计数= 0,如果没有结果匹配显示上面的计数。

+0

你真的想在mysql中这样做吗?有趣的任务,但没有太多真实的价值。 – 2012-03-12 19:33:29

+0

我正在为连接到我的数据库的另一个团队创建数据报告。我想提供数据的全部信息,并让他们简单地照顾UI部分。我意识到这是在这个功能的实际数据的细线之间,应该在后端处理,或者这是更多的显示任务,应该在前端处理。 – user391986 2012-03-12 19:38:55

+0

这是*绝对*前端任务...毫无疑问。 – 2012-03-12 20:31:03

回答

1

这可能是输入的一个奇怪的量,但会为你做它

select a.WeekNumber,ifnull(b.Count,0) Count 
from (select * from 
(select 1 WeekNumber union select 2 union select 3 union select 4 union select 5 
union select 6 union select 7 union select 8 union select 9 union select 10 
union select 11 union select 12 union select 13 union select 14 union select 15 
union select 16 union select 17 union select 18 union select 19 union select 20 
union select 21 union select 22 union select 23 union select 24 union select 25 
union select 26 union select 27 union select 28 union select 29 union select 30 
union select 31 union select 32 union select 33 union select 34 union select 35 
union select 36 union select 37 union select 38 union select 39 union select 40 
union select 41 union select 42 union select 43 union select 44 union select 45 
union select 46 union select 47 union select 48 union select 39 union select 50 
union select 51 union select 52 union select 53) aa) a 
left join mytable b using (WeekNumber) 
where WeekNumber between 2 and 35; 

下面是一些样本数据

mysql> drop database if exists user391986; 
Query OK, 1 row affected (0.03 sec) 

mysql> create database user391986; 
Query OK, 1 row affected (0.01 sec) 

mysql> use user391986 
Database changed 
mysql> CREATE TABLE mytable 
    -> (WeekNumber int,Count int,primary key(WeekNumber)); 
Query OK, 0 rows affected (0.06 sec) 

mysql> insert into mytable values (5,1),(8,2),(34,0); 
Query OK, 3 rows affected (0.04 sec) 
Records: 3 Duplicates: 0 Warnings: 0 

mysql> select * from mytable; 
+------------+-------+ 
| WeekNumber | Count | 
+------------+-------+ 
|   5 |  1 | 
|   8 |  2 | 
|   34 |  0 | 
+------------+-------+ 
3 rows in set (0.00 sec) 

mysql> 

下面是该查询执行

mysql> select a.WeekNumber,ifnull(b.Count,0) Count 
    -> from (select * from 
    -> (select 1 WeekNumber union select 2 union select 3 union select 4 union select 5 
    -> union select 6 union select 7 union select 8 union select 9 union select 10 
    -> union select 11 union select 12 union select 13 union select 14 union select 15 
    -> union select 16 union select 17 union select 18 union select 19 union select 20 
    -> union select 21 union select 22 union select 23 union select 24 union select 25 
    -> union select 26 union select 27 union select 28 union select 29 union select 30 
    -> union select 31 union select 32 union select 33 union select 34 union select 35 
    -> union select 36 union select 37 union select 38 union select 39 union select 40 
    -> union select 41 union select 42 union select 43 union select 44 union select 45 
    -> union select 46 union select 47 union select 48 union select 49 union select 50 
    -> union select 51 union select 52 union select 53) aa) a 
    -> left join mytable b using (WeekNumber) 
    -> where WeekNumber between 2 and 35; 
+------------+-------+ 
| WeekNumber | Count | 
+------------+-------+ 
|   2 |  0 | 
|   3 |  0 | 
|   4 |  0 | 
|   5 |  1 | 
|   6 |  0 | 
|   7 |  0 | 
|   8 |  2 | 
|   9 |  0 | 
|   10 |  0 | 
|   11 |  0 | 
|   12 |  0 | 
|   13 |  0 | 
|   14 |  0 | 
|   15 |  0 | 
|   16 |  0 | 
|   17 |  0 | 
|   18 |  0 | 
|   19 |  0 | 
|   20 |  0 | 
|   21 |  0 | 
|   22 |  0 | 
|   23 |  0 | 
|   24 |  0 | 
|   25 |  0 | 
|   26 |  0 | 
|   27 |  0 | 
|   28 |  0 | 
|   29 |  0 | 
|   30 |  0 | 
|   31 |  0 | 
|   32 |  0 | 
|   33 |  0 | 
|   34 |  0 | 
|   35 |  0 | 
+------------+-------+ 
34 rows in set (0.00 sec) 

mysql> 

给它是一个尝试!

+0

超级聪明!谢谢! – user391986 2012-03-12 20:20:52

0

WeekNumber是一个聚合字段还是一个列?您的问题可能平凡解,如:

SELECT week, count FROM mytable where week between 2 and 35 
+1

我不认为他正在寻找一个简单的'where' .. week = 2 - > count = 0 – 2012-03-12 19:34:17

+0

WeekNumber是GROUP BY WEEK(日期时间)的GROUPED字段 – user391986 2012-03-12 19:45:08

1

要做到这一点很容易在MySQL中不具有生成功能“独立”一系列连续的,你可以先创建一个表只用数字1-53与加盟,然后使用查询;

SELECT num WeekNumber, IFNULL(count,0) Count 
FROM MyTable 
RIGHT JOIN weeksequence 
    ON WeekNumber=num 
WHERE num BETWEEN 2 AND 35 
ORDER BY num; 

演示here