如何从结果集中获取计数?
How to get count from resultset?
ResultSet rs = statement.executeQuery("SELECT COUNT(DISTINCT `id`) FROM `users`");
在 MySQL 我有答案“5”,如何从 java 的结果集中得到这个 5?整数
将其作为 int 列获取:
int count = -1;
if (rs.next()) {
count = rs.getInt(1);
}
尝试为 COUNT(DISTINCT ID
) 添加别名,然后从结果集中检索别名。如果您稍后向查询添加更多逻辑,这将更具可读性和更易于维护。
ResultSet rs = statement.executeQuery("SELECT COUNT(DISTINCT `id`) as NUM_ROWS FROM `users`");
int count = -1;
if(rs.next())
{
count = rs.getInt("NUM_ROWS");
}
ResultSet rs = statement.executeQuery("SELECT COUNT(DISTINCT `id`) FROM `users`");
在 MySQL 我有答案“5”,如何从 java 的结果集中得到这个 5?整数
将其作为 int 列获取:
int count = -1;
if (rs.next()) {
count = rs.getInt(1);
}
尝试为 COUNT(DISTINCT ID
) 添加别名,然后从结果集中检索别名。如果您稍后向查询添加更多逻辑,这将更具可读性和更易于维护。
ResultSet rs = statement.executeQuery("SELECT COUNT(DISTINCT `id`) as NUM_ROWS FROM `users`");
int count = -1;
if(rs.next())
{
count = rs.getInt("NUM_ROWS");
}