将ResultSet用于其他查询

时间:2014-12-16 12:39:02

标签: java sql subquery resultset

我想知道是否可以在第二个查询中使用填充的ResultSet。 我正在考虑看起来像子查询的东西。

SELECT t.id, t.name FROM table t, (
    SELECT * FROM table2
    )
t2
WHERE t.id = t2.id;

在SQL-Logic中,它看起来像这样。我想像我的ResultSet一样使用我的ResultSet。

1 个答案:

答案 0 :(得分:0)

由于你只使用java结果集中的id,你可以将结果集循环到fecth id并在查询字符串中连接它们,如:

String query = "SELECT t.id, t.name FROM table t where t.id IN (";
while(rs.next()) {
  query += rs.getInt("id") + ",";
}
query += ")";
使用参数化查询

或更好