如何通过sql查询只保存数据库中的时间而不是日期

时间:2023-01-26 17:00:43

this query is saving complete date and time. but i want to save only time not date in database. is there any query to do this?

此查询正在保存完整的日期和时间。但我想在数据库中只保存时间而不是日期。有任何疑问吗?

update table set current_time=now();

2 个解决方案

#1


1  

Your column must be set to either DATETIME or TIMESTAMP.

您的列必须设置为DATETIME或TIMESTAMP。

If you use the TIME type then your query would work as expected.

如果您使用TIME类型,那么您的查询将按预期工作。

If you are using any other type of column then you could use CURTIME() method or CAST(column AS TIME) as mentioned by other answers, however this would use more space on disk, and make for much slower queries if you use to select, and prevent you from various operators:

如果您正在使用任何其他类型的列,那么您可以使用其他答案中提到的CURTIME()方法或CAST(列AS TIME),但是这将在磁盘上使用更多空间,并且如果您使用选择则会使查询速度慢得多,并阻止你从各种运营商:

e.g. SELECT * FROM table WHERE current_time<'12:00'

例如SELECT * FROM table WHERE current_time <'12:00'

You can see more information about the different DATE column types here: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html

您可以在此处查看有关不同DATE列类型的更多信息:https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html

Note that the CURTIME() method is not a standard SQL function, so this would only work on MySql

请注意,CURTIME()方法不是标准的SQL函数,因此这只适用于MySql

#2


0  

U can use CONVERT (time, SYSDATETIME()) as the value.

你可以使用CONVERT(time,SYSDATETIME())作为值。

This automates your process without using Current_time=now();

这样可以在不使用Current_time = now()的情况下自动化您的过程;

INSERT INTO table SET current_time = CONVERT (time, SYSDATETIME());

You can also use curtime();

你也可以使用curtime();

INSERT INTO table SET current_time = curtime();

Credits: Salmaan C

致谢:Salmaan C.

#1


1  

Your column must be set to either DATETIME or TIMESTAMP.

您的列必须设置为DATETIME或TIMESTAMP。

If you use the TIME type then your query would work as expected.

如果您使用TIME类型,那么您的查询将按预期工作。

If you are using any other type of column then you could use CURTIME() method or CAST(column AS TIME) as mentioned by other answers, however this would use more space on disk, and make for much slower queries if you use to select, and prevent you from various operators:

如果您正在使用任何其他类型的列,那么您可以使用其他答案中提到的CURTIME()方法或CAST(列AS TIME),但是这将在磁盘上使用更多空间,并且如果您使用选择则会使查询速度慢得多,并阻止你从各种运营商:

e.g. SELECT * FROM table WHERE current_time<'12:00'

例如SELECT * FROM table WHERE current_time <'12:00'

You can see more information about the different DATE column types here: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html

您可以在此处查看有关不同DATE列类型的更多信息:https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html

Note that the CURTIME() method is not a standard SQL function, so this would only work on MySql

请注意,CURTIME()方法不是标准的SQL函数,因此这只适用于MySql

#2


0  

U can use CONVERT (time, SYSDATETIME()) as the value.

你可以使用CONVERT(time,SYSDATETIME())作为值。

This automates your process without using Current_time=now();

这样可以在不使用Current_time = now()的情况下自动化您的过程;

INSERT INTO table SET current_time = CONVERT (time, SYSDATETIME());

You can also use curtime();

你也可以使用curtime();

INSERT INTO table SET current_time = curtime();

Credits: Salmaan C

致谢:Salmaan C.