Intellij Spring Boot 中的 psql 根据出生日期计算年龄
我将 Intellij 与 Spring Boot 一起使用。在我的 PSQL 数据库中,我有一个名为 dob(出生日期)的列,我想使用 @Query 注释从出生日期中提取人的年龄,并获取所有年龄超过 60 岁的男性。我尝试过这样做,但是没有结果。您有什么建议吗?
public interface PersonRepo extends JpaRepository<Person, Long> {
Person findByUsername(String username);
@Query("select new com.example.personservice.dto.MalesCovidOverSixty(p.gender,count(p.gender)) " +
"from Person p " +
"where p.gender='Male' " +
"and p.covid='t' " +
"and age(p.dob,current_date)>'20' " +
"group by p.gender " +
"order by count(p.gender) desc")
List<MalesCovidOverSixty>findMalesCovidOverSixty();
}
在 Postman 中,它给了我空括号 []。 在 Person.class 中,dob 设置为 LocalDate。 我的数据库中有一个 20 岁以上的男性,所以我开始尝试这个。
I use Intellij with Spring Boot. In my PSQL Database I have a column named as dob (date of birth) and I want with a @Query annotation extract the person's age from the birthdate and get all the male persons that have age over 60. I have tried to do it but with no result. Do you have any suggestions?
public interface PersonRepo extends JpaRepository<Person, Long> {
Person findByUsername(String username);
@Query("select new com.example.personservice.dto.MalesCovidOverSixty(p.gender,count(p.gender)) " +
"from Person p " +
"where p.gender='Male' " +
"and p.covid='t' " +
"and age(p.dob,current_date)>'20' " +
"group by p.gender " +
"order by count(p.gender) desc")
List<MalesCovidOverSixty>findMalesCovidOverSixty();
}
In Postman it gives me empty brancets [].
In Person.class dob is set as LocalDate.
I have a male person in my database who is over 20 so I tried this for beginning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的找到答案了!我发布它只是为了防止其他人遇到同样的问题。
Ok found the answer! I post it just in case someone else has the same problem.