Jump to content

Recommended Posts

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?

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.