我试着找到这个问题的解决方案,但是我发现的所有内容都要求稍微不同的问题,或者没有得到足够的答案。我有以下设置的表:使用Self-Join查找行之间的差异
fullvna
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| startdate | date | YES | | NULL | |
| starttime | time | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
+--------------+-------------+------+-----+---------+----------------+
我想找到每对连续行之间的时间差,所以= 1减去ID的开始时间= 2(表中有序ID的开始时间按时间顺序排列)。我根据我查询过什么,我在这里找到:http://www.mysqltutorial.org/mysql-tips/mysql-compare-calculate-difference-successive-rows/
create table difference as SELECT
one.id,
one.starttime,
two.starttime,
(one.starttime - two.starttime) AS diff
FROM
fullvna one
INNER JOIN
fullvna two ON two.id = one.id + 1;
我收到下面的打印输出,并且我不知道这意味着什么或者什么,我做错了:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near ' one.starttime,
two.starttime,
(one.starttime - two.starttime' at line 3
您应该使用[TIMEDIFF(表达式1,表达式2)(http://www.w3resource.com/mysql/date-and-time-functions/mysql-timediff-function.php) –
更改该行基本上返回相同的错误。现在日期和时间被分成两个单独的列而不是一个日期时间值。那是为什么? – Caitlin
没有意义。 \t [**如何创建最小,完整和可验证的示例**](http://stackoverflow.com/help/mcve) \t尝试在http://rextester.com中创建示例 –