mysql - 使用日期字符串作为参数的 sql 查询不起作用
问题描述
我正在尝试用 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);
但我得到同样的错误
然后我尝试将字符串转换为日期,但它没有用......有人可以告诉我我做错了什么吗?谢谢
解决方案
由于变量是字符串,因此它们作为 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);
推荐阅读
- vb.net - 有没有一种方法可以从 vb.net 中的数组生成随机字符串而不会重复
- jenkins - 如何在 Jenkins 上制作 groovy 以与凭据相同的方式屏蔽变量的输出?
- flutter - 如何在颤动中删除列表?
- c# - 在 MVC5 ASP.Net 应用程序中使用 Repository 和 UnitOfWork 模式时如何修改数据库初始化程序种子方法?
- c# - 我可以在 linux 容器中对 WCF 应用程序进行 dockerize 处理吗?
- ios - 使用“required init?(coder aDecoder: NSCoder)”进行 IBOutlets 初始化时,我会遇到任何问题吗?如果是,有什么例子?
- firebase - 有没有办法在 React Native Expo 中实现 Google OAuth 而不会弹出?
- amazon-web-services - AWS VPC - 无法从私有子网中的堡垒主机通过 SSH 连接到不同子网中同一 VPC 中的 EC2 实例
- python - Scrapy KeyError:'Spider not found',但在终端中执行'scrapy list'时存在蜘蛛
- excel - Visual Basic 不保存工作表