简体   繁体   中英

Why DATE function cannot use with count distinct in Hibernate?

Im using Hibernate with MySQL. I have the following query:

SELECT count(distinct DATE(s.my_date)) FROM my_table s WHERE (DATE(s.my_date) BETWEEN :from_date AND :to_date)"

Since I use Timestamp in MySQL, I need to use DATE() to compare date parts only. The problem is whenever I use DATE() along with the distinct keyword, I got errors:

org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '(' near line 1

The query worked fine if I removed the DATE() from the count() function but it produced wrong results. My question is why the DATE() works fine in Where clause but not in count() function with distinct keyword? I can get that worked with native query using DATE_FORMAT(), but for some reasons, Im trying to get things worked in Hibernate.

尝试执行以下查询: SELECT count(distinct DATE(s.my_date)) FROM my_table s WHERE DATE(s.my_date) BETWEEN :from_date AND :to_date"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM