我有输出到输出
的mysql命令+------------+---------------+
| WeekNumber | Count |
+------------+---------------+
| 5 | 1 |
| 8 | 2 |
| 34 | 0 |
+------------+---------------+
SELECT week, count
FROM mytable;
如何修改查询以便我可以提供一周范围(2 - 35)并且如果没有结果匹配则计数= 0,否则显示上面的计数。
答案 0 :(得分:1)
要在没有生成“独立”顺序系列功能的MySQL中轻松完成此操作,您可以先创建一个只有1-53到JOIN的表,然后使用查询;
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。
答案 1 :(得分: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>
试一试!!!
答案 2 :(得分:0)
WeekNumber是聚合字段还是列?你的问题可能很简单,如:
SELECT week, count FROM mytable where week between 2 and 35