简体   繁体   English

MYSQL:根据日期/小时将值从一个表传输到另一个表

[英]MYSQL: Transfer values from one table to another based on date/hour

i have the following MYSQL tables:我有以下 MYSQL 表:

Part of the input table 'input':输入表“输入”的一部分:

输入表

and part of the Output Table 'output':和输出表“输出”的一部分:

输出表

both date/time columns are pre given and cannot be changed.两个日期/时间列都是预先给定的,不能更改。 they are the basis of the MYSQL code I want to use to do the following: I want to sum all values from 'total' column of the input table from the same date and same hour and put them into 'total' column of the output table where the 'time' column has the same date and same hour as in the input table.它们是我想用来执行以下操作的 MYSQL 代码的基础:我想对来自同一日期和同一小时的输入表的 'total' 列中的所有值求和,并将它们放入输出的 'total' 列中'time' 列与输入表中的日期和小时数相同的表。

Would this be possible with MYSQL only?这仅适用于 MYSQL 吗? Or do I need PHP as well?还是我也需要 PHP? The input table has about 400,000 values.输入表有大约 400,000 个值。

I started with:我开始于:

SELECT SUM(total) FROM input
WHERE date BETWEEN '2019-06-06 00:00:00' AND '2019-06-06 23:59:59'

But I dont know how to continue.但我不知道如何继续。

Any help would be appreciated.任何帮助,将不胜感激。 Thanks in advance :)提前致谢 :)

Get the sums in the input table and join to the output table in the UPDATE statement:获取输入表中的总和并在UPDATE语句中连接到输出表:

update output o 
inner join (
  select date_format(date, '%Y%m%d%H') hour, sum(total) total
  from input
  group by date_format(date, '%Y%m%d%H')
) i on i.hour = date_format(o.date, '%Y%m%d%H')
set o.total = i.total
 UPDATE outputtable,
       (SELECT Sum(total)  SUM,
                Year(date)  year,
                Month(date) month,
                Day(date)   day,
                Hour(date)  hour
         FROM   inputtable
         GROUP  BY Year(date),
                   Month(date),
                   Day(date),
                   Hour(date)) InputT SET    total = InputT.sum WHERE  Year(outputtable.date) = InputT.year
        AND Month(outputtable.date) = InputT.month
        AND Day(outputtable.date) = InputT.day
        AND Hour(outputtable.date) = InputT.hour;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM