sum and union all query using mysql and java -


i have table following fields service_type , consumer_feedback.for example:

service_id  consumer_feedback 31           1 32           -1 33            1 31            1 32            1. 

i want find sum of consumer_feedback each service_id through java code

resultset res = st.executequery("select sum(consumer_feedback)                                      consumer1                                    service_id=31                                   union                                    select sum(consumer_feedback)                                      consumer1                                    service_id=32                                   union                                    select sum(consumer_feedback)                                      consumer1                                   service_id=33") ;     while (res.next()) {       int c1 = res.getint(1);                  sum1 = sum1 + c1;             }      system.out.println("sum of column "   +sum1);      while (res.next()) {       int c2 = res.getint(1);                  sum2 = sum2 + c2;                       }      system.out.println("sum of column "   +sum2);      while (res.next()) {       int c3 = res.getint(1);       sum3 = sum3 + c3;     }      system.out.println("sum of column "   +sum3); } 

but code working for2 service_id's , not 3 service_id's.please me out

your query should

select service_id,sum(consumer_feedback) consumer1 group service_id 

this eliminate need unions.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -