问题
I have two tables that I join on the id-column, they look like:
+-------+
| users |
+----+--+---+
| id | name |
+----+------+
+-------+
| posts |
+-------+------+---------+
| id | user_id | message |
+----+---------+---------+
And now I want to select all posts and include the username, with:
SELECT * FROM posts, users WHERE user_id = users.id
And then I try to get the values with:
ResultSet rs = // SQL
if(rs.next()) {
rs.getInt("posts.id");
...
}
But I get SQLException
when executing rs.getInt("posts.id")
:
java.sql.SQLException: Column 'posts.id' not found.
How can I get the values from the SQL-query above using JDBC, and JavaDB/Derby as database?
How can I distinguish between the id
column in the users
and posts
table when retrieving values with ResultSet
?
回答1:
You're attempting to retrieve the id
value, but you're using "posts.id" to reference it. Don't
All you need is the column name or alias, not the table name as well:
ResultSet rs = // SQL
if(rs.next()) {
rs.getInt("id");
...
}
It would've worked if your column name itself was "posts.id", but I recommend using underscore (_) instead of a period should you choose to update the table.
But I have an id column in both tables, how do i distinguish between them?
You need to specify a column alias:
SELECT p.id AS post_id,
p.name,
u.id AS users_id,
p.user_id, --redundant due to relationship, omit if possible
u.message
FROM POSTS p
JOIN USERS u ON u.id = p.user_id
...and reference that column alias in the Java code:
ResultSet rs = // SQL
if(rs.next()) {
rs.getInt("post_id");
...
}
回答2:
Solution 1 : use alias
select u.id as uid, u.name, p.id as post_id, p.user_id, p.message from
users u inner join posts p on u.id=p.user_id
Solution 2: remove the duplicate user.id since you already have it in the posts table
select p.user_id, u.name, p.id as post_id, p.message from
users u inner join posts p on u.id=p.user_id
回答3:
Alias the column names
SELECT
posts.id posts_id,
name name,
id id,
user_id,
message
FROM posts
INNER JOIN users
ON posts.user_id = users.id
depending on your sql flavour, that alias may need to be
posts.id as posts_id
回答4:
How can I get the values from the SQL-query above using JDBC, and JavaDB/Derby as database?
To get values, you should use the ResultSet
's column name. The column name used in the ResultSet
will be original column name or the alias.
If your ResultSet
has columns with the same name or alias, the getInt()
will return the first match.
With your select ...
SELECT * FROM posts, users WHERE user_id = users.id
...the rs.getInt()
will return only the posts.id
since it will the first column.
How can I distinguish between the id column in the users and posts table when retrieving values with ResultSet?
You can use a unique alias for every column or you can use the ResultSetMetaData
- Solution 1: Use SQL alias
SQL
SELECT p.id AS post_id,
p.name AS post_name,
u.id AS user_id,
u.message AS user_message
FROM POSTS p
JOIN USERS u
ON u.id = p.user_id
Java
ResultSet rs = // SQL
if(rs.next()) {
rs.getInt("post_id");
rs.getInt("user_id");
}
- Solution 2: Use
ResultSetMetaData
SQL
SELECT p.id
p.name
u.id
u.message
FROM POSTS p
JOIN USERS u
ON u.id = p.user_id
Java
Integer userID;
Integer postID;
ResultSetMetaData rsMeta = rs.getMetaData();
if (rs.next()) {
for (int i = 1; i <= rsMeta.getColumnCount(); i++) {
String table = rsMeta.getTableName(i);
String col = rsMeta.getColumnName(i);
if(table.equals("users") && col.equals("id")){
userID = rs.getInt(i);
continue;
}
if(table.equals("posts") && col.equals("id")) {
userID = rs.getInt(i);
continue;
}
}
}
回答5:
you can simplify these further by getting data as per the column index instead of getting it by column name..!! what ever data we retrieve from DB is stored in result set in the column order as per the select statement so instead of getting data as per the column name prefer fetching data as per the column index in result set..
ex:
String query = " select age,num from table_abc";
ResultSet rs= statement.executeQuery(query);
while(rs.next){
int var1= rs.getInt(1);
int var2= rs.getInt(2);
}
these will help you do away with complexity and confusion created due to similar column names in your database tables..
Hope these helps...
All the best..!!
来源:https://stackoverflow.com/questions/2970273/how-can-i-identify-columns-when-selecting-from-multiple-tables-with-jdbc