Mysql - 视图中每 n 行递增日期时间
Mysql - Increment datetime every n rows in a view
我有两个 table,我想每 n 行增加一个日期时间字段
jobs
| job_id | group_id | start_date | limit | delay |
+--------+----------+------------------+-------+--------+
| 1 | 1 | 2019-10-10 08:00 | 2 | 3600 |
| 2 | 1 | 2019-10-10 14:00 | 0 | 0 |
| 3 | 2 | 2019-10-10 14:00 | 1 | 1800 |
users
| user_id | group_id | user_name | row_num |
+---------+----------+------------+----------+
| xxx | 1 | Homer | 1 |
| xxx | 1 | Barney | 2 |
| xxx | 1 | Bart | 3 |
| xxx | 1 | Ned | 4 |
| xxx | 1 | Marge | 5 |
| xxx | 1 | Moe | 6 |
| xxx | 1 | Maggie | 7 |
| xxx | 2 | Lisa | 1 |
| xxx | 2 | Otto | 2 |
| xxx | 2 | Marge | 3 |
结果应该是这样
| job_id | group_id | user_name | start_date |
+----------+----------+-----------+-------------------+
| 1 | 1 | Homer | 2019-10-10 08:00 |
| 1 | 1 | Barney | 2019-10-10 08:00 |
| 1 | 1 | Bart | 2019-10-10 09:00 |
| 1 | 1 | Ned | 2019-10-10 09:00 |
| 1 | 1 | Marge | 2019-10-10 10:00 |
| 1 | 1 | Moe | 2019-10-10 10:00 |
| ... | ... | ... | ... |
| 2 | 1 | Homer | 2019-10-10 14:00 |
| 2 | 1 | Barney | 2019-10-10 14:00 |
| 2 | 1 | Bart | 2019-10-10 14:00 |
| 2 | 1 | Ned | 2019-10-10 14:00 |
| 2 | 1 | Marge | 2019-10-10 14:00 |
| 2 | 1 | Moe | 2019-10-10 14:00 |
| ... | ... | ... | ... |
| 3 | 2 | Lisa | 2019-10-10 14:00 |
| 3 | 2 | Otto | 2019-10-10 14:30 |
| 3 | 2 | Marge | 2019-10-10 15:00 |
| ... | ... | ... | ... |
为了实现这一点,我现在创建了一个 php 脚本来提取数据,制作一个循环并将它们插入到 table.
我想知道您是否只能通过使用 mysql 查询来做到这一点,我该怎么做。
谢谢
这是一种选择:
SELECT
j.job_id,
j.group_id,
u.user_name,
TIMESTAMPADD(HOUR, FLOOR((u.row_num-1) / 2), j.start_date) AS start_date
FROM jobs j
INNER JOIN users u
ON j.group_id = u.group_id
ORDER BY
j.job_id,
j.group_id,
u.row_num;
这里的逻辑是,我们在开始日期前增加一个小时,每增加两个 row_num
,从行号的第二步开始(即从 3 开始)。
我有两个 table,我想每 n 行增加一个日期时间字段
jobs
| job_id | group_id | start_date | limit | delay |
+--------+----------+------------------+-------+--------+
| 1 | 1 | 2019-10-10 08:00 | 2 | 3600 |
| 2 | 1 | 2019-10-10 14:00 | 0 | 0 |
| 3 | 2 | 2019-10-10 14:00 | 1 | 1800 |
users
| user_id | group_id | user_name | row_num |
+---------+----------+------------+----------+
| xxx | 1 | Homer | 1 |
| xxx | 1 | Barney | 2 |
| xxx | 1 | Bart | 3 |
| xxx | 1 | Ned | 4 |
| xxx | 1 | Marge | 5 |
| xxx | 1 | Moe | 6 |
| xxx | 1 | Maggie | 7 |
| xxx | 2 | Lisa | 1 |
| xxx | 2 | Otto | 2 |
| xxx | 2 | Marge | 3 |
结果应该是这样
| job_id | group_id | user_name | start_date |
+----------+----------+-----------+-------------------+
| 1 | 1 | Homer | 2019-10-10 08:00 |
| 1 | 1 | Barney | 2019-10-10 08:00 |
| 1 | 1 | Bart | 2019-10-10 09:00 |
| 1 | 1 | Ned | 2019-10-10 09:00 |
| 1 | 1 | Marge | 2019-10-10 10:00 |
| 1 | 1 | Moe | 2019-10-10 10:00 |
| ... | ... | ... | ... |
| 2 | 1 | Homer | 2019-10-10 14:00 |
| 2 | 1 | Barney | 2019-10-10 14:00 |
| 2 | 1 | Bart | 2019-10-10 14:00 |
| 2 | 1 | Ned | 2019-10-10 14:00 |
| 2 | 1 | Marge | 2019-10-10 14:00 |
| 2 | 1 | Moe | 2019-10-10 14:00 |
| ... | ... | ... | ... |
| 3 | 2 | Lisa | 2019-10-10 14:00 |
| 3 | 2 | Otto | 2019-10-10 14:30 |
| 3 | 2 | Marge | 2019-10-10 15:00 |
| ... | ... | ... | ... |
为了实现这一点,我现在创建了一个 php 脚本来提取数据,制作一个循环并将它们插入到 table.
我想知道您是否只能通过使用 mysql 查询来做到这一点,我该怎么做。
谢谢
这是一种选择:
SELECT
j.job_id,
j.group_id,
u.user_name,
TIMESTAMPADD(HOUR, FLOOR((u.row_num-1) / 2), j.start_date) AS start_date
FROM jobs j
INNER JOIN users u
ON j.group_id = u.group_id
ORDER BY
j.job_id,
j.group_id,
u.row_num;
这里的逻辑是,我们在开始日期前增加一个小时,每增加两个 row_num
,从行号的第二步开始(即从 3 开始)。