mysql - 为什么这段代码不起作用?从生成日期范围获取计数

标签 mysql date

我有两张表,一张是访问的表,唯一的房间表列出了房间的类型和房间的数量。

我想知道 2015-11-11 到 2015-11-12 的房间可用性,所以我制作了一系列数据并尝试编写如下脚本:

  1. 我做了一个日期范围:

  2. 然后我从一张表中搜索了信息,访问了每种类型的套房表上有多少未使用的房间

这是我的代码,用于获取日期范围在 2015-11-11 和 2015-11-12 之间的数据:

select a.Date, b.id_room, (b.num-count(c.datex)) as remain
from (
select curdate() - INTERVAL (a.a + (10 * b.a) - (100 * c.a)) DAY as Date
from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a cross join rooms b  join visit c on c.datex where a.Date between '2015-11-11' and '2015-11-12' group by b.id_room,a.Date

这是我的表格源

visit table
+----+------------+-------+----------+
| no | datex      | room  | guest_id |
+----+------------+-------+----------+
|  1 | 2015-11-12 | 10    |      200 |
|  5 | 2015-11-13 | 10    |      200 |
|  6 | 2015-11-14 | 10    |      200 |
|  7 | 2015-11-13 | 20    |      201 |
|  8 | 2015-11-16 | 20    |      300 |
|  9 | 2015-11-16 | 20    |      305 |
+----+------------+-------+----------+
6 rows in set (0.00 sec)

rooms
+---------+----------+-----+
| id_room | name     | num |
+---------+----------+-----+
|      10 | Standart |   2 |
|      20 | VIP      |   4 |
+---------+----------+-----+
2 rows in set (0.00 sec)

我期望的结果如下:

+----------+----------+--------+
|   date   | id_room  | remain |
+----------+----------+--------+
|2015-11-11|    10    |   2    |
|2015-11-11|    20    |   4    |
|2015-11-12|    10    |   1    |
|2015-11-12|    20    |   4    |
+----------+----------+--------+

但是从上面脚本的执行来看,结果并不像预期的那样。这是我的结果

+----------+----------+-------+
|   date   | id_room  |remain |
+----------+----------+-------+
|2015-11-11|    10    | -4    |
|2015-11-12|    10    | -4    |
|2015-11-11|    20    | -2    |
|2015-11-12|    20    | -2    |
+----------+----------+-------+

最佳答案

select a.Date,a.dop as id_room ,avail-ifnull(used.top,0) as remain
from (
select curdate() - INTERVAL (a.a + (10 * b.a) - (100 * c.a)) DAY as Date, id_room as dop, num as avail
from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
cross join (select id_room,num from rooms group by id_room) as e

) a left join (select datex,room,count(datex) as top from visit group by datex, room) as used on used.datex=a.Date where a.Date between '2015-11-11' and '2015-11-12' group by a.Date,a.dop;

关于mysql - 为什么这段代码不起作用?从生成日期范围获取计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33687166/

相关文章:

mysql - 如何在 Group By 后获得总计

mysql - SQL Group By 与另一列上的 Order By Last

mysql - Ruby On Rails 关于多对多关系的数据从 sqlite3 迁移到 mysql

c# - 为什么在集合中找不到 Mysql 存储过程参数?

php - Mysql返回行位置并排序

PHP:使用 DateTime 类转换日期

安卓获取日期

c# - 确定 UTC 时间是否在 DST 内

javax.el.E​​LException : Cannot convert 2015-12-03 18:50 of type class java. lang.String to class java.util.Date?

date - 将日期格式更改为 "%d/%m/%Y"