在 H2 上运行查询时未找到列“name_c”错误
Posted
技术标签:
【中文标题】在 H2 上运行查询时未找到列“name_c”错误【英文标题】:Column "name_c" not found error when running a query on H2 【发布时间】:2013-02-07 17:03:04 【问题描述】:我正在使用 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
谢谢,
【问题讨论】:
尝试将ORDER BY lower(name_c)
改为ORDER BY lower(name)
。
谢谢,但我必须使用 lower(name_c)。 lower(name) 工作正常。在我的实际查询中,name_c 不是直接列,而是从在 select 子句中运行的查询中计算得出的列。类似于:SELECT (SELECT name FROM other_users WHERE ...) AS name_c FROM users
【参考方案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);
【讨论】:
以上是关于在 H2 上运行查询时未找到列“name_c”错误的主要内容,如果未能解决你的问题,请参考以下文章