sql中一列的所有值的总和

时间:2021-04-09 07:57:44

I need to get the sum of all values of one column and put it in a textField. i tried the code below but i get an error. - The column name Sum(Price) is not valid.

我需要获取一列的所有值的总和并将其放在textField中。我尝试了下面的代码,但我收到一个错误。 - 列名称Sum(Price)无效。

     String sql="Select Sum(Price) from sold";
                              pst=con.prepareStatement(sql);
                              rs=pst.executeQuery();
                              if(rs.next()){
                                  String sum = rs.getString("Sum(Price)");
                                  textField_2.setText(sum);

com.microsoft.sqlserver.jdbc.SQLServerException: The column name Sum(Price) is not valid.

4 个解决方案

#1


2  

Use an alias:

使用别名:

 String sql="Select Sum(Price) as sumprice from sold";
                          pst=con.prepareStatement(sql);
                          rs=pst.executeQuery();
                          if(rs.next()){
                              String sum = rs.getString("sumprice");
                              textField_2.setText(sum);

I don't know if the values get converted correctly. The value is a number of some sort. Perhaps you want:

我不知道值是否正确转换。该值是某种数字。也许你想要:

 String sql="Select cast(Sum(Price) as varchar(255)) as sumprice from sold";

#2


1  

Use an alias for the calculated column

使用计算列的别名

Select Sum(Price) as sum_price from sold
...
String sum = rs.getString("sum_price");
textField_2.setText(sum);

#3


0  

Use the overload that takes the ordinal column number as the result of an aggregate is nameless by default: rs.getString(1)

使用带有序数列号的重载作为聚合的结果默认是无名的:rs.getString(1)

#4


0  

Try this.

        int sum = 0;
        Statement st = conn.createStatement();
        ResultSet res = st.executeQuery("SELECT SUM(price) FROM sold");
        while (res.next()) {
        int c = res.getInt(1);
        sum = sum + c;

        String str = Integer.toString(sum);
        textField_2.setText(str);

this will work

这会奏效

#1


2  

Use an alias:

使用别名:

 String sql="Select Sum(Price) as sumprice from sold";
                          pst=con.prepareStatement(sql);
                          rs=pst.executeQuery();
                          if(rs.next()){
                              String sum = rs.getString("sumprice");
                              textField_2.setText(sum);

I don't know if the values get converted correctly. The value is a number of some sort. Perhaps you want:

我不知道值是否正确转换。该值是某种数字。也许你想要:

 String sql="Select cast(Sum(Price) as varchar(255)) as sumprice from sold";

#2


1  

Use an alias for the calculated column

使用计算列的别名

Select Sum(Price) as sum_price from sold
...
String sum = rs.getString("sum_price");
textField_2.setText(sum);

#3


0  

Use the overload that takes the ordinal column number as the result of an aggregate is nameless by default: rs.getString(1)

使用带有序数列号的重载作为聚合的结果默认是无名的:rs.getString(1)

#4


0  

Try this.

        int sum = 0;
        Statement st = conn.createStatement();
        ResultSet res = st.executeQuery("SELECT SUM(price) FROM sold");
        while (res.next()) {
        int c = res.getInt(1);
        sum = sum + c;

        String str = Integer.toString(sum);
        textField_2.setText(str);

this will work

这会奏效