PostgreSQL:在时间戳记字段中选择一个喜欢的数据 [英] PostgreSQL: Select data with a like on timestamp field
问题描述
我正在尝试使用日期字段 date_checked(时间戳)上的赞从表中选择数据。但是我有这个错误:
I am trying to select data from a table, using a "like" on date field "date_checked" (timestamp). But I have this error :
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: timestamp without time zone
我的请求是:
SELECT my_table.id
FROM my_table
WHERE my_table.date_checker LIKE '2011-01-%'
我不想使用:
SELECT my_table.id
FROM my_table
WHERE my_table.date_checker >= '2011-01-01 00:00:00'
AND my_table.date_checker < '2011-02-01 00:00:00'
推荐答案
不想要使用<和>带有时间戳,但是这些运算符可以转换为索引扫描,并且可以进行字符串匹配...可以,但是可以使用EWWWW。
It's all very well not "wanting to use" < and > with timestamps, but those operators can be converted into index scans, and a string-match... well, it can, but EWWWW.
嗯,错误发生这种情况是因为您需要在使用字符串操作之前将时间戳显式转换为字符串,例如:
Well, the error is occurring because you need to explicitly convert the timestamp to a string before using a string operation on it, e.g.:
date_checker::text LIKE '2011-01-%'
而我假设然后在(date_checker :: text)
上创建索引,该表达式将成为索引扫描,但是.... EWWWW。
and I suppose you could then create an index on (date_checker::text)
and that expression would become an index scan but.... EWWWW.
这篇关于PostgreSQL:在时间戳记字段中选择一个喜欢的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!