1

I just tried to insert two rows with the current datetime and then calculated the elapsed time since that date. Here are the rows from my table after two insertions and using NOW() function to set the timestamp:

mysql> select * from pendingActivations;
+--------+------------+---------------------+
| userId | code       | timestamp           |
+--------+------------+---------------------+
|      2 | aaa        | 2010-08-23 17:04:02 |
|   2345 | alkfjkla23 | 2010-08-23 16:59:53 |
+--------+------------+---------------------+

Few minutes after the insertion of the row with userId equal to 2, I executed the following command which I hoped would give me the elapsed time from the timestamp for each row. Here are the results:

mysql> select userId, code, timestamp, NOW() - timestamp as elapsedSeconds from pendingActivations;
+--------+------------+---------------------+----------------+
| userId | code       | timestamp           | elapsedSeconds |
+--------+------------+---------------------+----------------+
|      2 | aaa        | 2010-08-23 17:04:02 |     136.000000 |
|   2345 | alkfjkla23 | 2010-08-23 16:59:53 |    4585.000000 |
+--------+------------+---------------------+----------------+

I wonder how the second row has that huge elapsedSeconds value which indicates that exactly 1 hour, 16 minutes and 25 seconds had passed, although it is easily seen that just around 5 minutes had passed since.

Here is the table structure:

mysql> describe pendingActivations;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| userId    | int(11)     | NO   | PRI | NULL    |       | 
| code      | varchar(32) | NO   | UNI | NULL    |       | 
| timestamp | datetime    | NO   |     | NULL    |       | 
+-----------+-------------+------+-----+---------+-------+

Any ideas and/or explanations?

4

1 に答える 1

3

問題を説明することはできませんが、-操作が予期しない形式で結果を返すのではないかと思います (マイクロ秒または 10 分の 1 を含む可能性があります)。

私は使用しますTIMEDIFF()

戻り値expr1expr2時間値として表されます。expr1 と expr2 は時刻または日時の式ですが、両方とも同じ型である必要があります。

于 2010-08-23T15:44:00.563 に答える