mysql循环选择

标签 mysql database

我有一个输出到 mysql 命令的输出

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

SELECT week, count 
FROM mytable;

我如何修改查询以便我可以提供周范围 (2 - 35) 并在没有结果匹配时使计数 = 0,否则显示上面的计数。

最佳答案

这可能是一个奇怪的输入量,但会为您完成

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>

试一试!!!

关于mysql循环选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9673265/

相关文章:

php - 保留时间(日历)返回错误值。

php - 如何使用友好的 URL?

java - Java Web 应用及其替代品的二级缓存

mysql - 如何在关系数据库中共享资源(特定情况)?

mysql - 创建查询数据库

php - 查找多个插入查询的 auto_increment 值

php - 每次在数据库中找到该字符串时,都将其显示出来

php - 防止值在同一天重复

php - 如何在socket.io和xampp phpmyadmin sql表之间连接

php - 加载文件中的数据错误