skip to Main Content
Name   Id
aa      1
bb      5

I use this to fetch all values from id column

SELECT id FROM name_table;

I want to sum the values in the id column. To be clear, I am making a financial application and want to add up all the money recorded in the msql database

2

Answers


  1. If you want to just get sum of all ID values, then this would work:

    SELECT SUM(id) FROM name_table
    
    Login or Signup to reply.
  2.     int sum = 0;
        while (res.next()) {
        int i = res.getInt(1); or rs.getString("id "); //using column name
        sum = sum + i;
        System.out.println(sum);
     OR
      String q= "SELECT SUM(id) as totalSum FROM tableName";
      PreparedStatement pst = connectio.prepareStatement(q);
      ResultSet rs = pst.executeQuery();
       if (rs.next()) {
        sum = rs.getString("totalSum");
       System.out.print("total sum" + sum);
        } else {
      System.out.print("Query didn't return any results");
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search