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 Link to comment https://forums.phpfreaks.com/topic/111762-solved-adding-items-in-same-field/ 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' Link to comment https://forums.phpfreaks.com/topic/111762-solved-adding-items-in-same-field/#findComment-573734 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 Link to comment https://forums.phpfreaks.com/topic/111762-solved-adding-items-in-same-field/#findComment-573739 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? Link to comment https://forums.phpfreaks.com/topic/111762-solved-adding-items-in-same-field/#findComment-573754 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. Link to comment https://forums.phpfreaks.com/topic/111762-solved-adding-items-in-same-field/#findComment-573756 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.