Hi
Here is a possible solution:
* First, create the test table with the test data:
Code:
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`tnow` datetime NOT NULL default '0000-00-00 00:00:00',
`tlater` datetime default NULL,
`timediff` int(10) unsigned default '0'
) TYPE=MyISAM;
INSERT INTO `test` (`tnow`, `tlater`, `timediff`) VALUES ('2003-07-24 05:57:52', '2003-07-24 05:59:52', NULL);
INSERT INTO `test` (`tnow`, `tlater`, `timediff`) VALUES ('2003-07-24 05:59:15', '2003-07-24 05:58:15', NULL);
INSERT INTO `test` (`tnow`, `tlater`, `timediff`) VALUES ('2003-07-24 05:59:00', NULL, NULL);
INSERT INTO `test` (`tnow`, `tlater`, `timediff`) VALUES ('2003-07-24 06:00:21', '2003-07-24 06:00:21', NULL);
INSERT INTO `test` (`tnow`, `tlater`, `timediff`) VALUES ('2003-07-24 06:01:04', '2003-07-24 06:01:06', 2);
* Next, test:
Code:
mysql> SELECT * FROM test;
+---------------------+---------------------+----------+
| tnow | tlater | timediff |
+---------------------+---------------------+----------+
| 2003-07-24 05:57:52 | 2003-07-24 05:59:52 | NULL |
| 2003-07-24 05:59:15 | 2003-07-24 05:58:15 | NULL |
| 2003-07-24 05:59:00 | NULL | NULL |
| 2003-07-24 06:00:21 | 2003-07-24 06:00:21 | NULL |
| 2003-07-24 06:01:04 | 2003-07-24 06:01:06 | 2 |
+---------------------+---------------------+----------+
5 rows in set (0.00 sec)
* Now, populate the `timediff` field. First, let's see which rows qualify for an UPDATE:
Code:
mysql> SELECT UNIX_TIMESTAMP( tlater ) - UNIX_TIMESTAMP( tnow ) AS timediff FROM test WHERE timediff IS NULL AND ( tnow < tlater OR tnow = tlater );
+----------+
| timediff |
+----------+
| 120 |
| 262800 |
+----------+
2 rows in set (0.00 sec)
There should not be to much confusion with the logic. Only row 1 and row 4 qualify in this example. Now for the actual UPDATE command:
Code:
mysql> UPDATE test SET timediff = ( UNIX_TIMESTAMP( tlater ) - UNIX_TIMESTAMP( tnow ) ) WHERE timediff IS NULL AND ( tnow < tlater OR tnow = tlater );
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0
* Finally, verify the data was updated correctly:
Code:
mysql> SELECT * FROM test;
+---------------------+---------------------+----------+
| tnow | tlater | timediff |
+---------------------+---------------------+----------+
| 2003-07-24 05:57:52 | 2003-07-24 05:59:52 | 120 |
| 2003-07-24 05:59:15 | 2003-07-24 05:58:15 | NULL |
| 2003-07-24 05:59:00 | NULL | NULL |
| 2003-07-24 06:00:21 | 2003-07-27 07:00:21 | 262800 |
| 2003-07-24 06:01:04 | 2003-07-24 06:01:06 | 2 |
+---------------------+---------------------+----------+
5 rows in set (0.00 sec)
Notes : The timediff column is of type INT, and contains the number of SECONDS `tnow` and `tlater` differs. To query this field, you can use the following:
Code:
mysql> SELECT timediff FROM test WHERE timediff IS NOT NULL;
+----------+
| timediff |
+----------+
| 120 |
| 262800 |
| 2 |
+----------+
3 rows in set (0.00 sec)
The `timediff` is of course the time difference in seconds.
References :
Cheers