sql - 在 h2 数据库上使用 GROUP BY 时出现 JdbcSQLException

标签 sql h2

当我在 h2 数据库(版本 1.3.161)上执行以下 sql 命令时:

CREATE TABLE Person
(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(10)
);

CREATE TABLE Actor
(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
PersonId int,
FOREIGN KEY (PersonId) REFERENCES Person(Id)
);


INSERT INTO Person (name) values ('A person');

INSERT INTO Actor ( PERSONID ) values (1);
INSERT INTO Actor ( PERSONID ) values (1);

SELECT ACTOR.ID, ACTOR.PERSONID FROM ACTOR GROUP BY PERSONID ;

我出现以下异常:

    org.h2.jdbc.JdbcSQLException: Column "ACTOR.ID" must be in the GROUP BY list; SQL statement:
SELECT ACTOR.ID, ACTOR.PERSONID FROM ACTOR GROUP BY PERSONID [90016-161]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
    at org.h2.message.DbException.get(DbException.java:169)
    at org.h2.message.DbException.get(DbException.java:146)
    at org.h2.expression.ExpressionColumn.updateAggregate(ExpressionColumn.java:155)
    at org.h2.command.dml.Select.queryGroupSorted(Select.java:183)
    at org.h2.command.dml.Select.queryWithoutCache(Select.java:610)
    at org.h2.command.dml.Query.query(Query.java:298)
    at org.h2.command.dml.Query.query(Query.java:268)
    at org.h2.command.dml.Query.query(Query.java:37)
    at org.h2.command.CommandContainer.query(CommandContainer.java:82)
    at org.h2.command.Command.executeQuery(Command.java:185)
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:173)
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:152)
    at org.h2.server.web.WebApp.getResult(WebApp.java:1311)
    at org.h2.server.web.WebApp.query(WebApp.java:1001)
    at org.h2.server.web.WebApp$1.next(WebApp.java:964)
    at org.h2.server.web.WebApp$1.next(WebApp.java:967)
    at org.h2.server.web.WebThread.process(WebThread.java:166)
    at org.h2.server.web.WebThread.run(WebThread.java:93)
    at java.lang.Thread.run(Thread.java:680)

如果我在 MYSQL 数据库上执行完全相同的命令,它会返回预期的结果:

 +----+----------+
 | id | PersonId |
 +----+----------+
 |  1 |        1 |
 +----+----------+

最佳答案

MySQL 是唯一允许这样做的数据库。我认为这是 MySQL 中的一个错误。另请参阅问题 Converting MySQL select to PostgreSQL , PostgreSQL GROUP BY different from MySQL? , PostgreSQL -must appear in the GROUP BY clause or be used in an aggregate function .

PostgreSQL 抛出以下异常:

ERROR: column "actor.id" must appear in the GROUP BY clause 
or be used in an aggregate function 
42803/0

Apache Derby 抛出以下异常:

Column reference 'ACTOR.ID' is invalid, or is part of an invalid expression.  
For a SELECT list with a GROUP BY, the columns and expressions being selected 
may only contain valid grouping expressions and valid aggregate expressions. 
42Y36/30000

(我没有用其他数据库进行测试)

关于sql - 在 h2 数据库上使用 GROUP BY 时出现 JdbcSQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20048107/

相关文章:

sql - Redshift 左外连接忽略空值

mysql - 在数据库中搜索具有多个 {tokens} 的字符串

mysql - SQL查询优化,如果第一个为空则按其他行排序

java - 如何检查 h2 数据库的健康和损坏

spring-boot - Spring Boot : Setting Hibernate naming strategy with @DataJpaTest and Flyway

java - 改变 H2 中的序列

sql - 在 mySQL 的 WHERE IN 子句中保持排序

mysql - SQL变量月转换日期

spring-data-jpa - 如何在使用 DataJpaTest 的 Spring Boot 2.0 测试中访问 H2 控制台

jdbc - 您可以在 H2 中创建多个目录吗?