2010-10-07 84 views

回答

11

是的,你可以比較datetimetimestamp。它不壞,但要注意以下事項:

請記住,雖然DATETIMEDATETIMESTAMP值都可以使用同一套的格式指定,其種類並不都具有相同的數值範圍。例如,TIMESTAMP值不能早於1970 UTC或晚於'2038-01-19 03:14:07' UTC。

這意味着,一個日期,例如'1968-01-01',法律作爲DATETIMEDATE值,同時,是不能作爲TIMESTAMP值有效,並且被轉換爲0

MySQL Reference Manual :: The DATETIME, DATE, and TIMESTAMP Types

注意如何以下測試用例一切工作正常:

CREATE TABLE t1 (d1 datetime, d2 timestamp); 

INSERT INTO t1 VALUES ('1968-01-01 00:00:00', '1980-01-01 00:00:00'); 
INSERT INTO t1 VALUES ('2040-01-01 00:00:00', '1980-01-01 00:00:00'); 

SELECT * FROM t1 WHERE d2 < d1; 
+---------------------+---------------------+ 
| d1     | d2     | 
+---------------------+---------------------+ 
| 2040-01-01 00:00:00 | 1980-01-01 00:00:00 | 
+---------------------+---------------------+ 
1 row in set (0.00 sec) 

SELECT * FROM t1 WHERE d2 > d1; 
+---------------------+---------------------+ 
| d1     | d2     | 
+---------------------+---------------------+ 
| 1968-01-01 00:00:00 | 1980-01-01 00:00:00 | 
+---------------------+---------------------+ 
1 row in set (0.00 sec) 

SELECT * FROM t1 WHERE d2 < '2040-01-01 00:00:00'; 
+---------------------+---------------------+ 
| d1     | d2     | 
+---------------------+---------------------+ 
| 1968-01-01 00:00:00 | 1980-01-01 00:00:00 | 
| 2040-01-01 00:00:00 | 1980-01-01 00:00:00 | 
+---------------------+---------------------+ 
2 rows in set (0.00 sec) 

SELECT * FROM t1 WHERE d2 > '2040-01-01 00:00:00'; 
Empty set (0.00 sec) 
+0

非常感謝。 :) – murvinlai 2010-10-07 22:49:43