title: MySQL - "insert if not exists" query?
body: <p>I'm trying to write a MySQL query which will update a blog post view counter if the user has not visited the post in the last 24 hours. I'm trying to write something like this (but this does not work):</p>
  
  <pre><code>  IF EXISTS (
       SELECT 1
       FROM `posts-views`
       WHERE
          `ip` = '".$_SERVER['REMOTE_ADDR']."'
          AND
          `time` &gt; ".($time-60*60*24)."
          AND
          `post` = $id
    ) THEN
    NULL
  
    ELSE
  
    INSERT INTO `posts-views`
    (`post`, `time`, `ip`, `user`)
    VALUES
    ($id, $time, '".$_SERVER['REMOTE_ADDR']."', $me)
  </code></pre>
  
  <p>What's the correct way to fix the query?</p>