是否可以将MySQL配置为在默认情况下将时间戳值作为UNIXTIMESTAMP返回,而不是在SELECT语句中强制转换每一列?
发布于 2013-03-20 00:43:33
MySQL有一个将日期转换为unix时间戳的函数。
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
mysql> SELECT UNIX_TIMESTAMP();
-> 1196440210
mysql> SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
-> 1196440219发布于 2013-03-20 00:43:51
您不能在MySQL配置中执行此操作。
你可以在应用程序级别上做到这一点-例如,在PHP中,你可以使用mysqli_result::fetch_fields()方法来检测时间戳类型并转换它,其他连接器也会有类似的方法。
或者,您可以按照建议,对时间戳列使用UNIX_TIMESTAMP()函数。
发布于 2013-03-20 00:58:21
听起来您似乎想要相同数据的不同视图:
mysql> select * from t;
+------+---------------------+
| data | ts |
+------+---------------------+
| foo | 2013-03-19 16:54:45 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> select data, unix_timestamp(ts) from t;
+------+--------------------+
| data | unix_timestamp(ts) |
+------+--------------------+
| foo | 1363712085 |
+------+--------------------+
1 row in set (0.00 sec)
mysql> create view tv (data, time_t) as select data, unix_timestamp(ts) from t;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tv;
+------+------------+
| data | time_t |
+------+------------+
| foo | 1363712085 |
+------+------------+
1 row in set (0.00 sec)https://stackoverflow.com/questions/15505303
复制相似问题