Java 如何在HQL中选择左侧分组(t字段,6)?
干净的SQL语法(MySQL)允许使用“按左(…)分组”进行选择 例如: 但如果我尝试使用HQL:Java 如何在HQL中选择左侧分组(t字段,6)?,java,mysql,sql,hql,Java,Mysql,Sql,Hql,干净的SQL语法(MySQL)允许使用“按左(…)分组”进行选择 例如: 但如果我尝试使用HQL: " select t from TableEntity t " + " where t.field1 = :field_1 and t.field2 = :field_2 " + " group by LEFT(t.field3, 6) " + " having COUNT(*) = :other_condition " 我有一个例外 org.hibernate.hql.ast.QuerySy
" select t from TableEntity t " +
" where t.field1 = :field_1 and t.field2 = :field_2 " +
" group by LEFT(t.field3, 6) " +
" having COUNT(*) = :other_condition "
我有一个例外
org.hibernate.hql.ast.QuerySyntaxException:意外标记:左侧靠近第1行第12列[
在这种情况下可以使用HQL吗?我认为您不能随心所欲,因为MySQL通过扩展
组,而其他数据库通常不支持这种扩展。您可以这样做:
SELECT substring(t.field3, 1, 6), count(*) as cnt
FROM `table` t
WHERE t.field1 = 777 AND t.field2 = 0
GROUP BY substring(t.field3, 1, 6)
HAVING COUNT(*) = 4;
请注意,分组依据
中的未聚合列也位于选择
中
SELECT substring(t.field3, 1, 6), count(*) as cnt
FROM `table` t
WHERE t.field1 = 777 AND t.field2 = 0
GROUP BY substring(t.field3, 1, 6)
HAVING COUNT(*) = 4;