从其他列中获取不同表的时间总和

时间:2018-02-10 13:28:23

标签: php mysql

有几张这样的表:

id   racer_nr  start_time`           arrive_time           spent_time
1    313       2018-02-04 10:00:00   2018-02-04 18:57:00   08:57:00
2    135       2018-02-04 10:00:00   2018-02-04 18:59:20   08:59:20
3    55        2018-02-04 10:00:00   2018-02-04 18:59:50   08:59:50
4    315       2018-02-04 10:00:00   2018-02-04 20:01:40   10:01:40

不同的表日不同。 需要从每个赛车手的所有表格中获得总计spent_time,并且我可以通过以下查询获得该值:

SELECT `race_day_1`.`racer_nr`, 
    sec_to_time(sum(spendsecs)) AS total_time_spent
    FROM
    (
        SELECT `race_day_1`.`racer_nr` , time_to_sec(`spent_time`) spendsecs
        FROM `race_day_1`

        UNION ALL
        SELECT `race_day_1`.`racer_nr` , time_to_sec(`spent_time`) spendsecs
        FROM `race_day_2` `race_day_1`

        UNION ALL
        SELECT `race_day_1`.`racer_nr` , time_to_sec(`spent_time`) spendsecs
        FROM `race_day_3` `race_day_1`

        UNION ALL
        SELECT `race_day_1`.`racer_nr` , time_to_sec(`spent_time`) spendsecs
        FROM `race_day_4` `race_day_1`

        UNION ALL
        SELECT `race_day_1`.`racer_nr` , time_to_sec(`spent_time`) spendsecs
        FROM `race_day_5` `race_day_1`
    ) `race_day_5`
GROUP BY `racer_nr`

我从here

获得的

但我真的希望得到每个表格(日期)start_time以及arrive_timespent_time ..是否可以在同一个查询中执行此操作?如果是的话 - 怎么样?

0 个答案:

没有答案