title: If then else within a select
body: <p>I am trying to make a script faster and I have decided to include some calculations that used to happen in PHP inside SQL. I have to make an if statement which decides whether to use <code>insert</code> or <code>update</code> if it's needed with the code:</p>
  
  <pre><code>IF (EXISTS (SELECT * FROM `edenteva` WHERE `Time`='1340252716' and `Code`='571119'))
  THEN IF (EXISTS (SELECT * FROM `map` WHERE `mega`='571119'))
  THEN UPDATE `map` SET `edenteva`='571119' WHERE `mega`='571119'
  ELSE INSERT INTO `map`(`mega`,`edenteva`) VALUES ('571119','571119');
  </code></pre>
  
  <p>But, I am getting a syntax error, can you see what is wrong?</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 'IF (EXISTS (SELECT * FROM <code>edenteva</code> WHERE <code>Time</code>='1340252716'
    and <code>Code</code>='57111' at line 1</p>
  </blockquote>