如何在 Java 的结果集中包含多个 Select 语句
How to have multiple Select Statements in a Result Set in Java
我目前 运行 遇到的问题是我想 select 一列并对值求和。然后我想在同一个table中总结另一列。目的是将这两个相加的列相减并得到输出。我对此的用途是休假。员工每周都会获得数小时的假期。然后当它们被使用时,它们在不同的列中。我需要计算出他们可以使用的剩余假期总数。
这就是我所拥有的:
try
{
//Getting Information FROM EMPLOYEE_TIME_LOG for Vacation
Statement vacationLogstmt = dbConn.createStatement();
ResultSet vacationLogSet = vacationLogstmt.executeQuery(
"(SELECT SUM(vacation_gained) FROM employee_time_log WHERE employee_id_number = " +userInputIdNumber + "), " +
"(SELECT SUM(vacation_used) FROM employee_time_log WHERE employee_id_number = " +userInputIdNumber + "), " +
"GROUP BY vacation_gained, vacation_used");
vacationLogSet.next();
String strVacationEarned = vacationLogSet.getString(1);
String strVacationUsed = vacationLogSet.getString(2);
Double vacationEarned = Double.parseDouble(strVacationEarned);
Double vacationUsed = Double.parseDouble(strVacationUsed);
Double totalVacation = (vacationEarned - vacationUsed);
String strTotalVacation =Double.toString(totalVacation);
txtVacationTotal.setText(strTotalVacation);
}
catch (SQLException e)
{
//throw new JboException(e);
System.err.println(e);
String connectionError = "Problem with the database, please contact MIS.";
JOptionPane.showMessageDialog(new JFrame(), connectionError, "Database Error",JOptionPane.ERROR_MESSAGE);
txtUserSignIn.setText("");
txtUserSignIn.requestFocus();
}
我收到一个错误:
java.sql.SQLSyntaxErrorException: ORA-00933: SQL 命令未正确结束
这甚至可以在一个结果集中完成吗?还是我只需要制作单独的结果集来执行此操作。
您的 sql 有几个问题。首先,不能有多个用逗号分隔的 select。相反,您应该有一个包含多列的 select。其次,您不应按 vacation_gained、vacation_used 分组,因为您在聚合函数中使用这些字段。 sql 应该如下:
"(SELECT SUM(vacation_gained), SUM(vacation_used) FROM employee_time_log WHERE employee_id_number = " + userInputIdNumber + ")";
或者,如果您只对剩余的休假时间感兴趣,您也可以将数学运算作为查询的一部分:
"(SELECT SUM(vacation_gained) - SUM(vacation_used) as total_vacation FROM employee_time_log WHERE employee_id_number = " + userInputIdNumber + ")";
我目前 运行 遇到的问题是我想 select 一列并对值求和。然后我想在同一个table中总结另一列。目的是将这两个相加的列相减并得到输出。我对此的用途是休假。员工每周都会获得数小时的假期。然后当它们被使用时,它们在不同的列中。我需要计算出他们可以使用的剩余假期总数。 这就是我所拥有的:
try
{
//Getting Information FROM EMPLOYEE_TIME_LOG for Vacation
Statement vacationLogstmt = dbConn.createStatement();
ResultSet vacationLogSet = vacationLogstmt.executeQuery(
"(SELECT SUM(vacation_gained) FROM employee_time_log WHERE employee_id_number = " +userInputIdNumber + "), " +
"(SELECT SUM(vacation_used) FROM employee_time_log WHERE employee_id_number = " +userInputIdNumber + "), " +
"GROUP BY vacation_gained, vacation_used");
vacationLogSet.next();
String strVacationEarned = vacationLogSet.getString(1);
String strVacationUsed = vacationLogSet.getString(2);
Double vacationEarned = Double.parseDouble(strVacationEarned);
Double vacationUsed = Double.parseDouble(strVacationUsed);
Double totalVacation = (vacationEarned - vacationUsed);
String strTotalVacation =Double.toString(totalVacation);
txtVacationTotal.setText(strTotalVacation);
}
catch (SQLException e)
{
//throw new JboException(e);
System.err.println(e);
String connectionError = "Problem with the database, please contact MIS.";
JOptionPane.showMessageDialog(new JFrame(), connectionError, "Database Error",JOptionPane.ERROR_MESSAGE);
txtUserSignIn.setText("");
txtUserSignIn.requestFocus();
}
我收到一个错误: java.sql.SQLSyntaxErrorException: ORA-00933: SQL 命令未正确结束
这甚至可以在一个结果集中完成吗?还是我只需要制作单独的结果集来执行此操作。
您的 sql 有几个问题。首先,不能有多个用逗号分隔的 select。相反,您应该有一个包含多列的 select。其次,您不应按 vacation_gained、vacation_used 分组,因为您在聚合函数中使用这些字段。 sql 应该如下:
"(SELECT SUM(vacation_gained), SUM(vacation_used) FROM employee_time_log WHERE employee_id_number = " + userInputIdNumber + ")";
或者,如果您只对剩余的休假时间感兴趣,您也可以将数学运算作为查询的一部分:
"(SELECT SUM(vacation_gained) - SUM(vacation_used) as total_vacation FROM employee_time_log WHERE employee_id_number = " + userInputIdNumber + ")";