timmah1 Posted June 25, 2008 Share Posted June 25, 2008 I have a database that has three fields (id, user, amount) I want to add all of the data in the amount field by user. I know how to add items and so forth, but I'm not sure how to do it this way. If there are four entries in the database id user amount 1 test 4 6 test1 8 2 test 9 3 test 8 How would I add all the amount fields for user test? thanks in advance Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted June 25, 2008 Share Posted June 25, 2008 Use a where clause... SELECT ... WHERE user = 'test' Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 25, 2008 Share Posted June 25, 2008 SELECT SUM(amount), user FROM table_name GROUP BY user cheers, Jay Quote Link to comment Share on other sites More sharing options...
timmah1 Posted June 25, 2008 Author Share Posted June 25, 2008 ok, obviously, I'm not doing this right $deposit_sql="SELECT SUM(amount), username, deposited, type FROM bank WHERE username = '$username' GROUP BY username"; $result = mysql_query($deposit_sql); $numrows = mysql_num_rows($result) or die("Sorry, there was a problem selecting your account ".mysql_error()); while($row = mysql_fetch_array($result)) { echo $row['amount']; } Nothing shows up Any insight? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted June 25, 2008 Share Posted June 25, 2008 SUM is an aggregate function, you can't use it when selecting other columns that are not aggregated. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.