使用 JDBC 从多个表中进行 SELECT 时如何识别列?

发布于 2024-09-03 18:33:01 字数 817 浏览 3 评论 0原文

我在 id 列上加入了两个表,它们看起来像:

+-------+
| users |
+----+--+---+
| id | name |
+----+------+

+-------+
| posts |
+-------+------+---------+
| id | user_id | message |
+----+---------+---------+

现在我想选择所有帖子并包含用户名,其中:

SELECT * FROM posts, users WHERE user_id = users.id

然后我尝试通过以下方式获取值:

ResultSet rs = // SQL
if(rs.next()) {
    rs.getInt("posts.id");
    ...
}

但我得到 SQLException< /code> 执行 rs.getInt("posts.id") 时:

java.sql.SQLException: Column 'posts.id' not found.

如何使用 JDBC 和 JavaDB/Derby 作为数据库从上面的 SQL 查询中获取值?

使用 ResultSet 检索值时,如何区分 usersposts 表中的 id 列?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

听风吹 2024-09-10 18:33:01

您正在尝试检索 id 值,但您正在使用“posts.id”来引用它。 不要

您需要的只是列名称或别名,而不是表名称:

ResultSet rs = // SQL
if(rs.next()) {
  rs.getInt("id");
  ...
}

如果您的列名称本身是“posts.id”,那么它就可以工作,但如果您选择更新表,我建议使用下划线 (_) 而不是句点。

但是我在两个表中都有一个 id 列,我如何区分它们?


您需要指定一个列别名:

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

...并在 Java 代码中引用该列别名:

ResultSet rs = // SQL
if(rs.next()) {
  rs.getInt("post_id");
  ...
}

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");
  ...
}
铁憨憨 2024-09-10 18:33:01

解决方案 1:使用别名

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

解决方案 2:删除重复的 user.id,因为您已经在 posts 表中拥有它

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

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
无所的.畏惧 2024-09-10 18:33:01

根据您的 sql 风格为列名设置别名

SELECT
    posts.id posts_id,
    name name,
    id id,
    user_id,
    message
FROM posts 
INNER JOIN users 
    ON posts.user_id = users.id

,该别名可能需要是

posts.id as posts_id

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
季末如歌 2024-09-10 18:33:01

如何使用 JDBC 和 JavaDB/Derby 作为数据库从上面的 SQL 查询中获取值?

要获取值,您应该使用 ResultSet 的列名称。 ResultSet 中使用的列名将是原始列名或别名。

如果您的 ResultSet 具有相同名称或别名的列,则 getInt() 将返回第一个匹配项。

通过您的选择...

SELECT * FROM posts, users WHERE user_id = users.id

...rs.getInt() 将仅返回 posts.id,因为它将是第一列。

使用 ResultSet 检索值时如何区分 users 和 posts 表中的 id 列?

您可以为每个列使用唯一的别名,也可以使用 ResultSetMetaData


  • 解决方案 1: 使用 SQL 别名

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");
}

  • 解决方案 2: 使用 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;
    }
  }
}

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;
    }
  }
}
_失温 2024-09-10 18:33:01

您可以通过根据列索引获取数据而不是通过列名称获取数据来进一步简化这些......!!我们从数据库检索的任何数据都根据 select 语句按列顺序存储在结果集中,因此不是根据列名称获取数据,而是根据结果集中的列索引获取数据。

例如:

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);
}

这些将帮助您消除由于数据库表中相似的列名而产生的复杂性和混乱。

希望这些有所帮助...祝

一切顺利..!

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..!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文