在MySQL中创建一个时间(半小时)表

时间:2016-04-19 20:10:52

标签: mysql date datetime mysql-5.5

出于报告目的,我需要一个时间表作为查找表,列出一天中的每半小时。除了它的时间之外,它的功能与日期表相同。

我找到了24小时表的一个很好的例子:

    CREATE TABLE hourly_data
(
    id INT PRIMARY KEY AUTO_INCREMENT,
    date DATE NOT NULL,
    hour INT NOT NULL,
    f1 INT NOT NULL,
    f2 INT NOT NULL,
    f3 INT NOT NULL,
    f4 INT NOT NULL,
    f5 INT NOT NULL,
    f6 INT NOT NULL,
    inserted timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)

但我宁愿每半小时一次。关于如何做到这一点的任何想法?这将有助于轮班并创建专门的报告。提前致谢!

2 个答案:

答案 0 :(得分:1)

我无法获得在 MYSQL 中工作的初始答案,而是打破了第二种老派方式。我使用以下查询替换了所有联合:

WITH RECURSIVE seq AS (SELECT 0 AS value UNION ALL SELECT value + 1 FROM seq WHERE value < 47) SELECT * FROM seq;

然后我调整了初始查询,使用上面的查询得到结果:

WITH RECURSIVE seq AS (SELECT 0 AS value UNION ALL SELECT value + 1 FROM seq WHERE value < 47) SELECT DATE('2010/01/01') + INTERVAL (value * 30) MINUTE AS mydate FROM seq AS parameter ORDER BY value;

只是时间列表:

WITH RECURSIVE seq AS (SELECT 0 AS value UNION ALL SELECT value + 1 FROM seq WHERE value < 47 ) select time(DATE('2010/01/01') + INTERVAL (value * 30) minute) AS Time FROM seq AS parameter ORDER BY value;

答案 1 :(得分:0)

在mariadb中你可以像这样直接使用序列引擎:

SELECT
  date('2010/01/01') + interval (seq * 30) Minute as mydate
FROM seq_0_to_10;

<强>示例

MariaDB [mysql]> select date('2010/01/01') + interval (seq * 30) Minute as mydate from seq_0_to_10;
+---------------------+
| mydate              |
+---------------------+
| 2010-01-01 00:00:00 |
| 2010-01-01 00:30:00 |
| 2010-01-01 01:00:00 |
| 2010-01-01 01:30:00 |
| 2010-01-01 02:00:00 |
| 2010-01-01 02:30:00 |
| 2010-01-01 03:00:00 |
| 2010-01-01 03:30:00 |
| 2010-01-01 04:00:00 |
| 2010-01-01 04:30:00 |
| 2010-01-01 05:00:00 |
+---------------------+
11 rows in set (0.00 sec)

MariaDB [mysql]>

没有SEQUENCE Engine的旧方法

SELECT
  DATE('2010/01/01') + INTERVAL (NR * 30) MINUTE AS mydate
FROM (
  SELECT d2.a*10+d1.a  AS nr FROM (
   SELECT 0  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 d1
   CROSS JOIN (
   SELECT 0 a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4  ) AS d2
WHERE d2.a*10+d1.a BETWEEN 0 AND 47
) AS parameter
ORDER BY nr;

<强>样品

MariaDB [(none)]> SELECT
    ->   DATE('2010/01/01') + INTERVAL (NR * 30) MINUTE AS mydate
    -> FROM (
    ->   SELECT d2.a*10+d1.a  AS nr FROM (
    ->    SELECT 0  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 d1
    ->    CROSS JOIN (
    ->    SELECT 0 a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4  ) AS d2
    -> WHERE d2.a*10+d1.a BETWEEN 0 AND 47
    -> ) AS parameter
    -> ORDER BY nr;
+---------------------+
| mydate              |
+---------------------+
| 2010-01-01 00:00:00 |
| 2010-01-01 00:30:00 |
| 2010-01-01 01:00:00 |
| 2010-01-01 01:30:00 |
| 2010-01-01 02:00:00 |
| 2010-01-01 02:30:00 |
| 2010-01-01 03:00:00 |
| 2010-01-01 03:30:00 |
| 2010-01-01 04:00:00 |
| 2010-01-01 04:30:00 |
| 2010-01-01 05:00:00 |
| 2010-01-01 05:30:00 |
| 2010-01-01 06:00:00 |
| 2010-01-01 06:30:00 |
| 2010-01-01 07:00:00 |
| 2010-01-01 07:30:00 |
| 2010-01-01 08:00:00 |
| 2010-01-01 08:30:00 |
| 2010-01-01 09:00:00 |
| 2010-01-01 09:30:00 |
| 2010-01-01 10:00:00 |
| 2010-01-01 10:30:00 |
| 2010-01-01 11:00:00 |
| 2010-01-01 11:30:00 |
| 2010-01-01 12:00:00 |
| 2010-01-01 12:30:00 |
| 2010-01-01 13:00:00 |
| 2010-01-01 13:30:00 |
| 2010-01-01 14:00:00 |
| 2010-01-01 14:30:00 |
| 2010-01-01 15:00:00 |
| 2010-01-01 15:30:00 |
| 2010-01-01 16:00:00 |
| 2010-01-01 16:30:00 |
| 2010-01-01 17:00:00 |
| 2010-01-01 17:30:00 |
| 2010-01-01 18:00:00 |
| 2010-01-01 18:30:00 |
| 2010-01-01 19:00:00 |
| 2010-01-01 19:30:00 |
| 2010-01-01 20:00:00 |
| 2010-01-01 20:30:00 |
| 2010-01-01 21:00:00 |
| 2010-01-01 21:30:00 |
| 2010-01-01 22:00:00 |
| 2010-01-01 22:30:00 |
| 2010-01-01 23:00:00 |
| 2010-01-01 23:30:00 |
+---------------------+
48 rows in set (0.00 sec)