我試着找到這個問題的解決方案,但是我發現的所有內容都要求稍微不同的問題,或者沒有得到足夠的答案。我有以下設置的表:使用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中創建示例 –