SQL Combine two Rows

Started on the old forum in April 2012 · view the original in the archive · old links redirect here

2 replies · 471 views · started 14 years ago · 1 min read

2 participants
rmkilc Newbie · 3 posts #1

Let's say I have a table like this to make things simple:

UUID	blockID	Destroyed	Placed

bob 	1 	107 		627
steve	1 	1560 		97
joe	1	136		11
bob 	2 	1294 		741
steve	2 	8121 		4465
joe	2	0		51
bob 	3 	138 		443
steve	3 	2475 		1783
joe	3	50		89
bob 	4 	2 		2
steve	4 	4 		35
joe	4	1		5

I want to combine the data for bob and steve into just steve. What SQL code would I use to merge bob and steve together per blockID, and for Destroyed and Placed, end up with the sum of of them added together?

something else Fellow · 2,296 posts #2

this is how i would do it with a little php:

$something = mysql_query("SELECT blockID, SUM(Destroyed) AS Destroyed, SUM(Placed) AS Placed FROM tablename WHERE UUID='steve' OR UUID='bob' GROUP BY blockID");
while ($else = mysql_fetch_array($something))
{
mysql_query("UPDATE tablename SET Destroyed='".$else['Destroyed']."', Placed='".$else['Placed']."' WHERE UUID='steve' AND blockID='".$else['blockID']."'");
}
rmkilc Newbie · 3 posts #3

It worked great! I modified it a bit but this got me through.

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close