Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在postgresql版本12中实现LIKE条件?_Sql_Postgresql_Spring Boot_Postgresql 12 - Fatal编程技术网

如何在postgresql版本12中实现LIKE条件?

如何在postgresql版本12中实现LIKE条件?,sql,postgresql,spring-boot,postgresql-12,Sql,Postgresql,Spring Boot,Postgresql 12,当我通过SpringBoot框架实现它时,这个查询显示错误,所以我在postgresql上检查了它,但它仍然显示错误 错误是:- Select COUNT(*) from Table_one where timestamp LIKE '%2020-03-04%' AND speed_type = 'SPEED'; 您需要将timestamp列强制转换为VARCHAR,以便将其与字符串进行比较: ERROR: operator does not exist: date ~~ unknown

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

错误是:-

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

您需要将
timestamp
列强制转换为
VARCHAR
,以便将其与字符串进行比较:

 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

您需要将
时间戳
列强制转换为
VARCHAR
,以便将其与字符串进行比较:

 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

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

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

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

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

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

timestamp::text LIKE '%2020-03-04%'
其中时间戳>='2020-03-04'和
时间戳<'2020-03-05'和
速度类型=‘速度’
这不仅可以防止不必要的类型转换。但它也不受可能影响字符串转换(可能取决于数据库)的任何国际化设置的影响


它还可以使用适当的索引,包括
时间戳
。而且优化器有更好的信息,这可以改进查询计划(尽管本例中的计划很简单)。

使用日期/时间函数!不要转换成字符串

timestamp::text LIKE '%2020-03-04%'
其中时间戳>='2020-03-04'和
时间戳<'2020-03-05'和
速度类型=‘速度’
这不仅可以防止不必要的类型转换。但它也不受可能影响字符串转换(可能取决于数据库)的任何国际化设置的影响


它还可以使用适当的索引,包括
时间戳
。而且优化器有更好的信息,这可以改进查询计划(尽管本例中的计划很简单).

我需要在Spring中使用此查询我需要在Spring中使用此查询
时间戳类似于“%2020-03-04%”
时间戳='2020-03-04'
不同,如果
时间戳
是一个日期?将时间戳或日期与之进行比较确实是一个问题,非常糟糕的想法如果时间戳是一个日期,那么像“%2020-03-04%”这样的时间戳与
timestamp='2020-03-04'
有什么不同呢?比较时间戳和日期是一个非常非常非常糟糕的想法。我正试图用spring做这件事,我也在用spring做这件事