如何通过SQL查询在数据库中仅保存时间而不保存日期 [英] how to save only time not date in database through sql query

查看:98
本文介绍了如何通过SQL查询在数据库中仅保存时间而不保存日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此查询正在保存完整的日期和时间.但我只想保存时间而不是数据库中的日期.有任何查询可以做到这一点吗?

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();

推荐答案

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

Your column must be set to either DATETIME or TIMESTAMP.

如果使用TIME类型,则查询将按预期工作.

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

如果您正在使用任何其他类型的列,则可以使用其他答案提到的CURTIME()方法或CAST(column AS 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:

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

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

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

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

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

这篇关于如何通过SQL查询在数据库中仅保存时间而不保存日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆