我们是否应该使用 CAST
来比较下面的查询日期?
在此讨论mysql between operator with dates上说将参数STRING转换为DATE更好,但WHERE子句中的mysql函数不会使性能变慢吗?
SELECT * from tableA WHERE datetimecolumn = CAST('2014-10-12' AS DATE)
答案 0 :(得分:0)
以这种方式使用它:
SELECT *
from tableA
WHERE datetimecolumn = STR_TO_DATE('2014-10-12', '%Y-%m-%d');
文档...... http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_str-to-date
好的......所以这就是为什么。mysql> select * from temp;
+---------------------+
| dt |
+---------------------+
| 2014-10-11 23:30:53 |
+---------------------+
2 rows in set (0.00 sec)
你也可以这样做......但你需要一个功能。它不会受伤。
SELECT * FROM `calendar` WHERE DATE_FORMAT(startTime, "%Y-%m-%d") = '2010-04-29'
SELECT * FROM `calendar` WHERE DATE(startTime) = '2010-04-29'
从这里开始:https://stackoverflow.com/a/2758585/623952
这就是我最终的结果...所以我想,如果你知道你的确切日期时间。用它。
mysql> select count(*) from temp;
+----------+
| count(*) |
+----------+
| 399998 |
+----------+
1 row in set (0.11 sec)
mysql> select count(*) from temp where temp_dt = date('2014-10-17');
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.15 sec)
mysql> select count(*) from temp where date_format(temp_dt, "%Y-%m-%d") = '2014-10-17';
+----------+
| count(*) |
+----------+
| 57590 |
+----------+
1 row in set (0.17 sec)
mysql> select count(*) from temp where substring(temp_dt,0,10) = '2014-10-17';
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.14 sec)
mysql> select count(*) from temp where temp_dt = '2014-10-17 17:06:20';
+----------+
| count(*) |
+----------+
| 9 |
+----------+
1 row in set (0.12 sec)