SQL 如何只选择昨天的记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1574565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I select records ONLY from yesterday?
提问by Keith Myers
I've spent hours searching the web for an answer to this question...
我花了几个小时在网上搜索这个问题的答案......
Here's what I currently have:
这是我目前拥有的:
select *
from order_header oh
where tran_date = sysdate-1
回答by OMG Ponies
Use:
用:
AND oh.tran_date BETWEEN TRUNC(SYSDATE - 1) AND TRUNC(SYSDATE) - 1/86400
Reference: TRUNC
参考:TRUNC
Calling a function on the tran_date
means the optimizer won't be able to use an index (assuming one exists) associated with it. Some databases, such as Oracle, support function based indexes which allow for performing functions on the data to minimize impact in such situations, but IME DBAs won't allow these. And I agree - they aren't really necessary in this instance.
调用函数tran_date
意味着优化器将无法使用与其关联的索引(假设存在索引)。某些数据库(例如 Oracle)支持基于函数的索引,这些索引允许对数据执行函数以最大程度地减少这种情况下的影响,但 IME DBA 不允许这样做。我同意 - 在这种情况下它们并不是真正必要的。
回答by Henry Gao
trunc(tran_date) = trunc(sysdate -1)
回答by Pablo Santa Cruz
to_char(tran_date, 'yyyy-mm-dd') = to_char(sysdate-1, 'yyyy-mm-dd')
回答by ninesided
If you don't support future dated transactions then something like this might work:
如果您不支持未来过时的交易,那么这样的事情可能会起作用:
AND oh.tran_date >= trunc(sysdate-1)
回答by netsuvi
This comment is for readers who have found this entry but are using mysql instead of oracle! on mysql you can do the following: Today
这个评论是给那些找到这个条目但使用mysql而不是oracle的读者的!在 mysql 上,您可以执行以下操作: 今天
SELECT *
FROM
WHERE date(tran_date) = CURRENT_DATE()
Yesterday
昨天
SELECT *
FROM yourtable
WHERE date(tran_date) = DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)
回答by Aliuk
If you want the timestamp for yesterday try something like:
如果您想要昨天的时间戳,请尝试以下操作:
(CURRENT_TIMESTAMP - INTERVAL '1' DAY)