SQL postgres - 如何在加入后重命名每一列,因为它们的字段太多

SQL postgres - How to rename each column after join because their are too many fields

我想加入两个 table 并阅读 java 中的所有列。在 java 中,结果集包含所有字段,但它们在两个 table 中具有相同的名称。 它们大约有 65 列,我不想在 select.

之后指定每一列

SQL查询:

select * from Tab a join Tab b on a.id = b.id;

在java我想要:

private MyObject map(ResultSet rs) throws SQLException {
    myObj.setNameA(rs.getString("a_name"));
    myObj.setNameB(rs.getString("b_name"));
}

我的想法是用 table 的变量重命名每一列,例如 'a_name' 和 'b_name',然后读取 java 中的字段。

我可以重命名 table 的每个字段而不指定 select 之后的每一列吗? (喜欢 'select a.name as a_name, b.name as b_name,...')

或者他们是否有更好的解决方案来获取 java 中的所有列?

您可以使用列号代替列名。 Javadoc 明确指出 (http://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html)

When a getter method is called with a column name and several columns have the same name, the value of the first matching column will be returned. The column name option is designed to be used when column names are used in the SQL query that generated the result set. For columns that are NOT explicitly named in the query, it is best to use column numbers. If column names are used, the programmer should take care to guarantee that they uniquely refer to the intended columns, which can be assured with the SQL AS clause.

当然,使用列号需要知道列的确切位置(基于 1)。

private MyObject map(ResultSet rs) throws SQLException {
    myObj.setNameA(rs.getString(1));
    myObj.setNameB(rs.getString(66));
}