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.
or is there any way of retrieving only time from CURRENT_TIMESTAMP
??
Try combining TIME()
and NOW()
:
SELECT TIME(NOW());
Output: 15:20:31
Of course, your column will need to be of type " TIME ".
You can use MySQL's built-in TIME()
function to extract the time part of a datetime expression. 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)
.
创建表时,将列类型设置为TIME
If I understand your question correctly, I think you have a timestamp column that has a default value of 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:
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. Or you can just use CURRENT_TIMESTAMP, and get only the time part with TIME function:
SELECT TIME(yourTimestampColumn)
FROM yourTable
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.