SQL 查询到 return 具有多个同名列的结果集的可能性

Possibility of SQL query to return a result set that has more than one column with the same name

来自 Oracle 文档 http://docs.oracle.com/javase/1.5.0/docs/guide/jdbc/getstart/resultset.html

In some cases, it is possible for an SQL query to return a result set that has more than one column with the same name. If a column name is used as the parameter to a getXXX method, getXXX will return the value of the first matching column name.

有人知道 "some cases" 吗?

这是一个常见的例子:

SELECT * FROM table1 JOIN table2 ON table1.table1Id = table2.table1Id;

这里你知道 table1table2 有一个名为 table1Id 的列,并且它保证具有相同的值。但是,如果您有自加入:

SELECT * FROM employee e JOIN employee m ON e.manager_id = m.id;

现在您遇到了问题,您的结果集可能没有多大意义。

最简单的情况是

select 1 as A, 2 as A from dual

与某人可能(不情愿地)在复杂查询中创建相等别名的方式非常相似。

假设两个表定义为

  • 作者:AuthorId、FirstName、LastName、Title...
  • 图书:图书编号、书名、ISBN、作者编号...

人们希望使用以下方式列出带有作者信息的书籍 SQL

SELECT *
FROM Books
JOIN Authors USING (AuthorId)

现在 ResultSet 中有两个不相关的 title 列,一个是书名,一个是作者的(例如学术)头衔。