title: PHP MySQL SUM mulitple-columns with 0/1 marker in a table
body: <p>I like to build a summary from my database.</p>
  
  <p>Table:writenquotes</p>
  
  <pre><code>id|  datum   |member|transaktion |pm|beträge
  1 |1372456800|Name1 |transaktion1|0 |  24
  2 |1372543200|Name2 |transaktion1|1 |  24
  </code></pre>
  
  <p>With my Code:</p>
  
  <pre><code>&lt;?php
  include("dbopen.php");
  $result = mysql_query("SELECT SUM(beträge)
  FROM writenquotes");
  while($row=mysql_fetch_array($result))
  {
  echo $row['SUM(beträge)'];
  }
  ?&gt;
  </code></pre>
  
  <p>My problem is, the PM declares the positive or negative figures with 0 or 1.</p>
  
  <p>0 = positive figures</p>
  
  <p>1 = negative figures</p>
  
  <p>With my code, I can build only a positive summary e.g. 48...</p>
  
  <p>id 1 = 24 ( positive )</p>
  
  <p>id 2 = -24 ( negative )</p>
  
  <p>The summary it would be real 0 and not 48</p>
  
  <p>Don't know what is my mistake but my code gives me only a 0 ( Zero ) or a Resource id #3 ... umpf :-(</p>
  
  <pre><code>&lt;?php
  include("dbopen.php");
  
  $result = mysql_query("SELECT SUM(IF(pm=1, -1, 1) * beträge) FROM  writenquotes", $link);
  echo mysql_errno($link) . " " . mysql_error($link). "\n";
  while($row=mysql_fetch_array($result))
  {
  echo "&lt;font color=\"red\"&gt;".$row['SUM(beträge)']."&lt;/font&gt;";
  }
  ?&gt;
  </code></pre>
  
  <p>And this </p>
  
  <pre><code>&lt;?php
  include("dbopen.php");
  $result = mysql_query("SELECT SUM(CASE pm=0 THEN beträge ELSE -beträge END) FROM writenquotes", $link);
  echo mysql_errno($link) . " " . mysql_error($link). "\n";
  echo $result['SUM(beträge)'];
  ?&gt;
  </code></pre>
  
  <p>gives me only this:</p>
  
  <blockquote>
    <p>1064 You have an error in your SQL syntax; check the manual that
    corresponds to your MySQL server version for the right syntax to use
    near 'THEN beträge ELSE -beträge END) FROM writenquotes' at line 1</p>
  </blockquote>