所以,我有一个表,其中“ABC”列是时间戳,“BCD”列是日期时间。
如果我这样做:
SELECT * FROM myTable WHERE ABC > BCD
不好吗?会影响性能吗?
它们在性能方面的比较如何?
最佳答案
是的,您可以将 datetime
与 timestamp
进行比较。这还不错,但要注意以下几点:
Remember that although
DATETIME
,DATE
, andTIMESTAMP
values all can be specified using the same set of formats, the types do not all have the same range of values. For example,TIMESTAMP
values cannot be earlier than1970 UTC
or later than'2038-01-19 03:14:07'
UTC.This means that a date such as
'1968-01-01'
, while legal as aDATETIME
orDATE
value, is not valid as aTIMESTAMP
value and is converted to 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)
关于mysql - 我可以将 MysQL 时间戳与日期时间列进行比较吗?不好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3886269/