简体   繁体   中英

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

From oracle doc 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.

Anybody knows about "some cases"?

The simplest case is

select 1 as A, 2 as A from dual

much the same way someone may (unwillingly) create equal aliases in a complex query.

Here's a common example:

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

Here you know table1 and table2 have a column named table1Id , and it is guaranteed to have the same value. However if you have a self-join:

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

Now you have a problem and, probably, your result set will not make as much sense.

Imagine two tables defined as

  • Authors : AuthorId, FirstName, LastName, Title...
  • Books : BookId, Title, ISBN, AuthorId...

where one would want to list books with author information using the following SQL

SELECT *
FROM Books
JOIN Authors USING (AuthorId)

Now you have two unrelated title columns in the ResultSet , one being the title of the book and one being the (eg academic) title of the author.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM