title: How to merge rows from a MySQL database table
body: <h2>EDIT: I reformulated the question because it seemed too complicated the other way.</h2>
  
  <p>Consider the following database tables</p>
  
  <p><strong>users</strong></p>
  
  <pre><code>#id #money
  1   100
  2   150
  3   100
  4   200
  5   200
  6   100
  7   250
  8   300
  9   150
  10  100
  </code></pre>
  
  <p><strong>merged</strong></p>
  
  <pre><code>#mainID #mergeID
  1       2
  1       3
  </code></pre>
  
  <p>How do I formulate a Select statement which sums the money of the merged ID's and makes a top10 list? My real database has around 100'000 id/money rows and around a hundred of merges. This means it's important to keep performance in mind.</p>
  
  <p>This is what I wish to get as output in this very example:</p>
  
  <pre><code>#id #money
  1   350    // sum of the rows with id 1,2,3 because they're merged!
  8   300
  7   250
  4   200
  5   200
  9   150
  6   100
  10  100
  </code></pre>