java - 在 2 个表中使用具有相同列名的 ResultSetMetaData

标签 java sql database sybase resultset

我想从 2 个表中获取列名,然后使用 ResultSetMetaData 获取它们的值和ResultSet 。假设表 myTable 有 x、y 和 z 列。我愿意

 Select * from table myTable A,myTable B where A.x = B.y

我想在我的代码中使用 B.z 的值。

ResultSetMetaData会将列列为 x y z x y z ,那么如何区分 z 的值与别名 A 和 B 呢?

我需要在查询中使用 *,因为它可以提高我的性能。所以我不能在那里提及列名。有什么方法可以使用 ResultSet 的列名获取 B.z 列的值?我的意思是有什么办法ResultSetMetaData会将列列为 A.x、A.y、A.z、B.x、B.y 和 B.z?

最佳答案

如果您不能或不想指定列名(带有别名),那么您将需要按索引而不是按列标签检索值。 ResultSet API doc明确表示:

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.

(强调我的)

如果你想区分列,那么你可以使用 ResultSetMetaData.getTableName ,但它不会帮助您进行检索(除了找到要使用的“正确”索引)。请注意,并非所有数据库都支持提供表名称。

关于java - 在 2 个表中使用具有相同列名的 ResultSetMetaData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30937762/

相关文章:

php - 如何在数据库的单个查询中调用 2 个相关表

java - Java中如何获取字符的精确边界框

java - BufferedReader 没有读取整个字符串

sql - 如何在 SQL Server 中以整数形式创建日期、月份和年份

php - 我需要一个好的 mysql 查询来接收来自 3 个表的信息

sql - `single-row subquery returns more than one row` 错误不清楚

mysql - 表之间有空格的 SELECT 语句?

python - 使用 Django 使用与 ORM 数据库相同的连接来查询 MySQL 数据库

java - 为什么我的 TopComponent 上的 JavaFX 内容消失了?

Java - 如何检测IP版本