English 中文(简体)
我如何整整整天与时日实地相匹配?
原标题:How do I match an entire day to a datetime field?

我有一张表可供对比。 该表有一个栏目,名称为<代码>matchdate,即<代码>datetime field。

如果我在2011-12-01年有3个对应点:

  • 2011-12-01 12:00:00
  • 2011-12-01 13:25:00
  • 2011-12-01 16:00:00

我怎么问? 我如何询问所有时间在1个日期是否一致?

I have looked at date_trunc(), to_char(), etc.
Isn t there some "select * where datetime in date" function?

最佳回答

Cast You timestamp 如果你想要简单的条码,则对<条码>的价值。 与此类似:

SELECT *
FROM   tbl
WHERE  timestamp_col::date =  2011-12-01 ;  -- date literal

然而,随着大张表的推移,速度将加快:

SELECT *
FROM   tbl
WHERE  timestamp_col >=  2011-12-01 0:0     -- timestamp literal
AND    timestamp_col <   2011-12-02 0:0 ;

理由:第二点不必改变表格中的每一单项价值,并且能够使用时间段一栏的简单索引。 The expression is sargable

Note excluded the upper bound (< instead of <=) for a correct selection.
You can make up for that by creating an
index on an expression like this:

CREATE INDEX tbl_ts_date_idx ON tbl (cast(timestamp_col AS date));

然后,第一期问询的速率会越快。

问题回答

不要确定我是否在这里失踪了明显的东西,但我认为,你只能这样做。

select * from table where date_trunc( day , ts) =  2011-12-01 ;

仅使用BETWEEN这样的功能:

SELECT * FROM table WHERE date BETWEEN  2011-12-01  AND  2011-12-02 

你可能需要在字面上注明时间,但这应当包括爱的极限,排除上。

我认为,你可以做:

.where(:between =>  2011-12-01 .. 2011-12-02 )




相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签