在我的 Postgresql 資料庫中,我有一列:
date_emailed timestamp NULL
當我將它插入表中時,我使用 date_emailed 列的值“now()”。這是示例資料:

當我嘗試查詢以值“2022-10-17 06:39:10.036”突出顯示的行時,我僅在使用 >= 而不是 = 時才獲得該值。例如:
使用“>=”:
select date_emailed
from passdown.category_account_entry
where
date_emailed is not null
and date_emailed >= '2022-10-17 06:39:10.036'::timestamp
order by date_emailed desc;
我的結果回傳多行,這在使用 '>=' 時是正確的:

但是當我嘗試只使用 '=' 來獲取確切的行時,我得到零結果:
select date_emailed
from passdown.category_account_entry
where
date_emailed is not null
and date_emailed = '2022-10-17 06:39:10.036'::timestamp
order by date_emailed desc;
所以我想我會把它轉換成一個紀元測驗,我注意到 date_emailed 的紀元值與字串值 '2022-10-17 06:39:10.036' 不同
select date_emailed,
date_part('epoch', date_emailed) as epoch_from_date_emailed,
date_part('epoch', '2022-10-17 06:39:10.036'::timestamp) as from_value
from passdown.category_account_entry
where
date_emailed is not null
and date_emailed >= '2022-10-17 06:39:10.036'::timestamp
order by date_emailed desc;

那么字串值 '2022-10-17 06:39:10.036' 與 date_emailed 中的資料有什么不同呢?我錯過了什么嗎?任何提示或建議將不勝感激。
uj5u.com熱心網友回復:
正如您在示例資料中看到的那樣,這是一個精度問題,因此使用 date_trunc 函式可以解決
select date_trunc('milliseconds', date_emailed)
= date_trunc('milliseconds', '2022-10-17 06:39:10.036'::timestamp)
from passdown.category_account_entry;
所以現在這句話是真的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/517282.html
標籤:PostgreSQL
