SQL Combine two Rows

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    SQL Combine two Rows

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

    Code:
    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?

    #2
    this is how i would do it with a little php:
    PHP Code:
    $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']."'");

    Comment


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

      Comment

      Working...
      X