Вот небольшой пример с таблицей из 3 столбцов (id, timestamp, cnt)
, тогда вы можете рассчитать его следующим образом:
SELECT p.id, p.mytime, p.photons, l.photons
,TIMESTAMPDIFF(SECOND, l.mytime, p.mytime) diffsecs
,p.photons * COALESCE(TIMESTAMPDIFF(SECOND, l.mytime, p.mytime),1) as cnt
FROM photons p
LEFT JOIN photons l ON p.id -1 = l.id
ORDER BY p.id;
SELECT SUM( p.photons * COALESCE(TIMESTAMPDIFF(SECOND, l.mytime, p.mytime),1)) as cnt
FROM photons p
LEFT JOIN photons l ON p.id -1 = l.id
ORDER BY p.id;
sample
mysql> describe photons;
+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| mytime | timestamp | YES | MUL | NULL | |
| photons | int(11) | YES | | NULL | |
+---------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> select * from photons;
+----+---------------------+---------+
| id | mytime | photons |
+----+---------------------+---------+
| 1 | 2020-02-26 12:00:00 | 100 |
| 2 | 2020-02-26 12:00:25 | 80 |
| 3 | 2020-02-26 12:01:00 | 100 |
| 4 | 2020-02-26 12:05:00 | 200 |
| 5 | 2020-02-26 12:05:03 | 22 |
+----+---------------------+---------+
5 rows in set (0.00 sec)
mysql> SELECT p.id, p.mytime, p.photons, l.photons
-> ,TIMESTAMPDIFF(SECOND, l.mytime, p.mytime) diffsecs
-> ,p.photons * COALESCE(TIMESTAMPDIFF(SECOND, l.mytime, p.mytime),1) as cnt
-> FROM photons p
-> LEFT JOIN photons l ON p.id -1 = l.id
-> ORDER BY p.id;
+----+---------------------+---------+---------+----------+-------+
| id | mytime | photons | photons | diffsecs | cnt |
+----+---------------------+---------+---------+----------+-------+
| 1 | 2020-02-26 12:00:00 | 100 | NULL | NULL | 100 |
| 2 | 2020-02-26 12:00:25 | 80 | 100 | 25 | 2000 |
| 3 | 2020-02-26 12:01:00 | 100 | 80 | 35 | 3500 |
| 4 | 2020-02-26 12:05:00 | 200 | 100 | 240 | 48000 |
| 5 | 2020-02-26 12:05:03 | 22 | 200 | 3 | 66 |
+----+---------------------+---------+---------+----------+-------+
5 rows in set (0.00 sec)
mysql> SELECT SUM( p.photons * COALESCE(TIMESTAMPDIFF(SECOND, l.mytime, p.mytime),1)) as cnt
-> FROM photons p
-> LEFT JOIN photons l ON p.id -1 = l.id
-> ORDER BY p.id;
+-------+
| cnt |
+-------+
| 53666 |
+-------+
1 row in set (0.00 sec)
mysql>