Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在HQL中选择左侧分组(t字段,6)?_Java_Mysql_Sql_Hql - Fatal编程技术网

Java 如何在HQL中选择左侧分组(t字段,6)?

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

干净的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.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;