HSQLDB:找不到列

HSQLDB: column not found

我正在使用 HSQLDB 为我的应用程序编写一些功能测试。我的应用生成以下 SQL 请求:

SELECT 
CLIENT.account_id, 
CLIENT.client_code, 
SHIPPINGADDRESS10A9.account_id, 
SHIPPINGADDRESS10A9.address_code
FROM CLIENT 
LEFT JOIN ADDRESS SHIPPINGADDRESS10A9 
    ON SHIPPINGADDRESS10A9.address_code = CLIENT.shipping_address_code AND (SHIPPINGADDRESS10A9.account_id = 4) 
WHERE CLIENT.account_id = 4

此请求有效并且适用于我的代码 (JDBC + MySQL)。当我尝试使用 HSQLDB 执行我的功能测试时,出现以下异常:

resultSet.findColumn("CLIENT.account_id"); //Works
resultSet.findColumn("SHIPPINGADDRESS10A9.account_id"); //Exception

Caused by: java.sql.SQLException: Column not found: SHIPPINGADDRESS10A9.account_id
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCResultSet.findColumn(Unknown Source)
    at com.inventis.bw.data.DataBaseUtil.loadBean(DataBaseUtil.java:419)
    ... 33 more
Caused by: org.hsqldb.HsqlException: Column not found: SHIPPINGADDRESS10A9.account_id
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    ... 36 more

知道我做错了什么吗?

编辑: 看起来问题出在 table 别名。

logger.debug("address account id => " + resultSet.findColumn("ADDRESS.account_id")); // Works
logger.debug("address account id => " + resultSet.findColumn("SHIPPINGADDRESS10A9.account_id")); //Exception

使用 table 名称访问列值非常有效,但如果我使用 table 别名,则会抛出异常。有什么想法吗?

感谢您的帮助。

我想您的问题是两列具有相同的别名 "account_id",这会造成混淆。尝试给他们不同的名字:

SELECT CLIENT.account_id as client_account_id,
       CLIENT.client_code, 
       SHIPPINGADDRESS10A9.account_id as shipping_account_id
       SHIPPINGADDRESS10A9.address_code

只是好奇,你可以这样写查询吗?

SELECT c.account_id as client_account_id,
       c.client_code, 
       a.account_id as shipping_account_id
       a.address_code
FROM CLIENT c LEFT JOIN
     ADDRESS a 
     ON a.address_code = c.shipping_address_code AND 
        a.account_id = c.account_id
WHERE c.account_id = 4;

这应该是等价的,并且清楚地表明您不需要在 select.

中 return account_id 两次

您应该尝试这个 SQL 请求来正确声明连接:

SELECT 
C.account_id, 
C.client_code, 
S.account_id, 
S.address_code
FROM CLIENT C,ADDRESS S
WHERE S.address_code = C.shipping_address_code AND C.account_id = 4

你会试试这个

resultSet.findColumn("C.account_id"); 
resultSet.findColumn("S.account_id"); 

我喜欢戈登·利诺夫的回答。我只是想补充一点,如果你想让它在 HSQLDB

中工作,你需要在连接字符串“;get_column_name=false”中指定 __