2012-03-12 57 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