0
Select COUNT(*) from Table_one where timestamp LIKE '%2020-03-04%' AND speed_type = 'SPEED';

当我通过 Spring Boot 框架实现它时,这个查询显示错误,所以我在 postgresql 上检查了它,但它仍然显示错误。

错误是这样的: -

 ERROR:  operator does not exist: date ~~ unknown
LINE 1: Select COUNT(*) from table_one where timestamp LIKE '2020-0...
                                                        ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 49
4

3 回答 3

2

使用日期/时间功能!不要转换为字符串!

where timestamp >= '2020-03-04' AND
      timestamp < '2020-03-05' AND
      speed_type = 'SPEED'

这不仅可以防止不必要的类型转换。但它也不受任何可能影响转换为字符串(可能取决于数据库)的国际化设置的影响。

它还可以使用适当的索引,包括timestamp. 并且优化器有更好的信息,可以改进查询计划(尽管这种情况下的计划,诚然,很简单)。

于 2020-03-05T13:02:40.757 回答
0

因为您将时间戳值与字符串类型值进行比较。因此,需要进行转换,例如

timestamp::text LIKE '%2020-03-04%'
于 2020-03-05T05:06:54.300 回答
0

您需要将timestamp列转换为VARCHAR允许将其与字符串进行比较:

Select COUNT(*) 
from Table_one 
where CAST(timestamp AS VARCHAR) LIKE '%2020-03-04%'
  AND speed_type = 'SPEED';

dbfiddle 上的演示

于 2020-03-05T05:04:41.867 回答