Jump to content

[SOLVED] mysql select


alin19

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/126733-solved-mysql-select/#findComment-655484
Share on other sites

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 />";
}

Link to comment
https://forums.phpfreaks.com/topic/126733-solved-mysql-select/#findComment-655494
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.