1

是否可以将 MySQL 配置为默认将 TIMESTAMP 值作为 UNIXTIMESTAMP 返回,而不是在 SELECT 语句中强制转换每一列?

4

3 回答 3

1

MySQL has a function to convert a date to a unix timestamp.

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-19T16:43:33.347 回答
0

You cannot do that in MySQL configuration.

You can do that on application level - e.g. in PHP, you can use the mysqli_result::fetch_fields() method to detect timestamp type and convert it, other connectors will have similar methods.

Or you can do it - as suggested - using UNIX_TIMESTAMP() function on timestamp columns.

于 2013-03-19T16:43:50.717 回答
0

听起来好像您想要相同数据的不同视图:

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)
于 2013-03-19T16:58:20.957 回答