【问题标题】:sql query with string of a date as parameter doesn't work使用日期字符串作为参数的 sql 查询不起作用
【发布时间】:2021-12-14 11:49:35
【问题描述】:

我正在尝试使用 jpa 编写查询,但我不能。 我试过了:

@Query(value="select count(id_ass) from assenza where id_dip=?1 and data_ass between ?2 and ?3", nativeQuery = true)
Integer getAssenzeByid_dipmese(int id_dip,String inizio, String fine);

但我得到ERROR: operator does not exist: date >= character varying

然后我尝试使用

 @Query(value="select count(id_ass) from assenza where id_dip=?1 and data_ass >= ?2 and  data_ass<= ?3", nativeQuery = true)
Integer getAssenzeByid_dipmese(int id_dip,String inizio, String fine);

但我得到同样的错误

然后我尝试转换日期中的字符串,但没有成功... 有人可以告诉我我做错了什么吗?谢谢

【问题讨论】:

  • 哪种格式有 inizio 和 fine 。 mysql要2021-10-29
  • 格式是2021-10-29,需要在字符串末尾加'-'吗?
  • 但我得到错误:运算符不存在:日期>=字符变化这不是 MySQL 错误消息。检查您的 DBMS。
  • 我也怀疑mysql是这里的问题,不,你不需要添加单引号

标签: mysql sql spring-boot hibernate jpa


【解决方案1】:

由于变量是字符串,它们作为 varchars 传递。 您必须将 varchars 显式转换为日期。

@Query(value="select count(id_ass) from assenza where id_dip=?1 and data_ass between CAST(?2 AS DATE) and CAST(?3 AS DATE)", nativeQuery = true)
Integer getAssenzeByid_dipmese(int id_dip,String inizio, String fine);

【讨论】:

  • 抱歉刚刚看到答案,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多