PostgreSQL中的日期比较 [英] Date comparison in PostgreSQL
本文介绍了PostgreSQL中的日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法像这样比较同一张表中的两个日期:
Is there a way to compare two dates in the same table like this:
SELECT * FROM mytable WHERE date_1 = date_2;
我正在寻找最简单的方法作为更新语句的一部分。
I am looking for the simplest approach to do this as part of an update statement.
推荐答案
是的,您可以这样做。如此简单。
Yes, you can do that. Simple as that.
Have a look at the date
/ time
data types in PostgreSQL.
如果您的日期实际上是时间戳
,则可能需要特别注意。默认情况下,最多保存6个小数位,使用相等运算符可能会比较棘手。您可以强制转换为 timestamp(0)
舍入为秒,或使用 date_trunc()
截断为不同时间之一单位。
You may have to pay special attention if your "date", in fact, is a timestamp
. By default up to 6 fractional digits are saved, can be tricky with the equality operator. You might cast to timestamp(0)
to round to seconds or use date_trunc()
to truncate to one of various time units.
这篇关于PostgreSQL中的日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文