1

我正在使用 H2 的内存实例进行单元测试,并且正在尝试运行以下查询:

SELECT name AS name_c FROM users ORDER BY lower(name_c)

(真正的查询要复杂得多,但这个简单的查询具有相同的症状)

我收到以下异常:

org.h2.jdbc.JdbcSQLException: Column "NAME_C" not found; SQL statement:
SELECT name AS "name_c" FROM users ORDER BY lower(name_c) [42122-163]
    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.optimize(ExpressionColumn.java:138)
    at org.h2.expression.Function.optimize(Function.java:1705)
    at org.h2.command.dml.Select.prepare(Select.java:799)
    at org.h2.command.Parser.prepareCommand(Parser.java:218)
    at org.h2.engine.Session.prepareLocal(Session.java:415)
    at org.h2.engine.Session.prepareCommand(Session.java:364)
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1121)
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:164)
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:152)
    at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)
    at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)
    at com.name.dal.Connection.executeQuery(Connection.java:462)
    at com.name.model.users.UserTest.testTest(UserTest.java:324)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.rules.RunRules.evaluate(RunRules.java:18)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

有谁知道为什么?

我正在使用以下连接字符串创建数据库:

jdbc:h2:mem:db1;MODE=MYSQL;DB_CLOSE_DELAY=60;IGNORECASE=TRUE

谢谢,

4

1 回答 1

1

列别名仅在按order by原样(单独)使用时才在子句中可用。这与其他数据库的行为相同(根据我的测试,PostgreSQL 和 Derby)。有一些数据库确实支持您想要的(例如 MySQL),但这不是 SQL 标准的一部分。例如:

create table test(id int primary key, name varchar(255));
insert into test values(1, 'hello');
insert into test values(2, 'world');

这适用于我测试的所有数据库:

select name as c_name from test order by c_name;
select name as c_name from test order by lower(name);

有了这些数据,我对 PostgreSQL、Apache Derby 和 H2 的以下查询失败了。它适用于 MySQL:

select name as c_name from test order by lower(c_name);
于 2013-02-07T22:02:46.117 回答