简体   繁体   English

是否有任何可能的方法来存储当前时间只能通过phpmyadmin在mysql中

[英]is there any possible way to store current time only via phpmyadmin in mysql

I want to store current time (for example 00:30) in one of my column , the default CURRENT_TIMESTAMP gives both date and time which i dont want. 我想将当前时间(例如00:30)存储在我的一个列中,默认的CURRENT_TIMESTAMP给出了我不想要的日期和时间。

or is there any way of retrieving only time from CURRENT_TIMESTAMP ?? 或者有没有办法只从CURRENT_TIMESTAMP中检索时间?

Try combining TIME() and NOW() : 尝试结合TIME()NOW()

SELECT TIME(NOW());

Output: 15:20:31 输出: 15:20:3115:20:3115:20:31

Of course, your column will need to be of type " TIME ". 当然,您的列必须是“ TIME ”类型。

You can use MySQL's built-in TIME() function to extract the time part of a datetime expression. 您可以使用MySQL的内置TIME()函数来提取日期时间表达式的时间部分。 For example: 例如:

mysql> select time(now());
+-------------+
| time(now()) |
+-------------+
| 18:21:38    |
+-------------+
1 row in set (0.03 sec)

Since CURRENT_TIMESTAMP is a synonym for NOW() , you can just use TIME(CURRENT_TIMESTAMP) . 由于CURRENT_TIMESTAMPNOW()的同义词,因此您只需使用TIME(CURRENT_TIMESTAMP)

创建表时,将列类型设置为TIME

If I understand your question correctly, I think you have a timestamp column that has a default value of CURRENT_TIMESTAMP: 如果我正确理解您的问题,我认为您有一个时间戳列,其默认值为CURRENT_TIMESTAMP:

CREATE TABLE yourTable (
  ...,
  ...,
  currentTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

If you just want to store the current time, and not the date part, you could use a TIME column and trigger, that will insert the time part automatically: 如果您只想存储当前时间而不是日期部分,则可以使用TIME列和触发器,它将自动插入时间部分:

CREATE TABLE tableName (
  ...,
  ...,
  currentTime TIME
);

CREATE TRIGGER currentTime BEFORE INSERT ON tableName
FOR EACH ROW
  SET new.currentTime = TIME(NOW());

(an example is here ) and you might also want to use an UPDATE trigger. 这里有一个例子)你可能也想使用UPDATE触发器。 Or you can just use CURRENT_TIMESTAMP, and get only the time part with TIME function: 或者你可以使用CURRENT_TIMESTAMP,并只获得带TIME函数的时间部分:

SELECT TIME(yourTimestampColumn)
FROM yourTable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM