use COUNT(*) to count multiple rows from multiple tables in netbean
use COUNT(*) to count multiple rows from multiple tables in netbean
So I want to count the total number of sales for each seller from two different tables.
And I wrote down the code but it does not work..... Can anyone please help me???
So it should produce that sellerID =1 and count = 13. sellerID=2 and count= 14. Something like this. So I want to produce 3 total amount of sales for each seller, but the output shows the red lines ....
String sellerID = {"1","2","3"};
int num = 0;
String totalAmountSale = new String[3];
String sql = "SELECT tblOrder.SellerID, COUNT(*) " +
"FROM tblOrder, tblSeller " +
"WHERE tblOrder.SellerID = tblSeller.SellerID " +
"GROUP BY tblOrder.SellerID"
+ "ORDER BY tblOrder.SellerID;";
ResultSet rs = db.query(sql);
try {
while(rs.next())
{
totalAmountSale[num]= ""+rs.getInt(2);
num++;
}
rs.close();
} catch (SQLException ex) {
Logger.getLogger(DatabaseWork.class.getName()).log(Level.SEVERE, null, ex);
}
String out = "";
for (int i = 0; i < 3; i++) {
out+= totalAmountSale[i]+"n";
}
JOptionPane.showMessageDialog(null,out );
}
1 Answer
1
You miss a space:
String sql = "SELECT tblOrder.SellerID, COUNT(*) " +
"FROM tblOrder, tblSeller " +
"WHERE tblOrder.SellerID = tblSeller.SellerID " +
"GROUP BY tblOrder.SellerID "
+ "ORDER BY tblOrder.SellerID;";
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you elaborate on "it does not work"? Are you getting an error? The wrong results?
– Mureinik
Jun 29 at 9:25