alin19 Posted October 2, 2008 Share Posted October 2, 2008 i have a mysql table that looks like this: name value ion 25 vasile 20 ion 25 vasile 10 alex 5 i need to make a selection by name, and add the values for each name, example of result: ion 50 vasile 30 alex 5 how can i do that? Quote Link to comment https://forums.phpfreaks.com/topic/126733-solved-mysql-select/ Share on other sites More sharing options...
waynew Posted October 2, 2008 Share Posted October 2, 2008 Em, $query = mysql_query("UPDATE users SET value = 50 WHERE name = 'Vasile'") or die(mysql_error()); I think thats what you wanted? Quote Link to comment https://forums.phpfreaks.com/topic/126733-solved-mysql-select/#findComment-655483 Share on other sites More sharing options...
alin19 Posted October 2, 2008 Author Share Posted October 2, 2008 nop, i think that i didn't explain to good i need a select query that get's me all name's from that table, and for each name to get a sum of all values inserted there, i've thinked of something but i need two queryes: $select=mysql_query("select * from `table` group by `name`"); foreach ($select as $test) $sum=mysql_qyery("select sum(value) from `table` where name=$test"); and i think that can be done with only one query Quote Link to comment https://forums.phpfreaks.com/topic/126733-solved-mysql-select/#findComment-655484 Share on other sites More sharing options...
budimir Posted October 2, 2008 Share Posted October 2, 2008 You probably want this: $sql = "SELECT field, SUM(*) as values FROM tablename WHERE NAME = '$testname'"; Try it out... Quote Link to comment https://forums.phpfreaks.com/topic/126733-solved-mysql-select/#findComment-655493 Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 Like this? $query = mysql_query("SELECT name, SUM(value) AS total FROM table_name GROUP BY name ORDER BY total DESC") or die(mysql_error()); Then just run a while loop to fetch all the records. Like so: while($r = mysql_fetch_array($query)){ echo $r['name'].": ".$r['total']."<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/126733-solved-mysql-select/#findComment-655494 Share on other sites More sharing options...
alin19 Posted October 2, 2008 Author Share Posted October 2, 2008 10x ProjectFear, this is what i needed Quote Link to comment https://forums.phpfreaks.com/topic/126733-solved-mysql-select/#findComment-655499 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.