title: Mysql stored procedure if count is never null
body: <p>I got a table that is being updated every 5 Minutes by a Script Call, triggered by a http request. The table looks like this </p>
  
  <pre><code>MariaDB &gt; select host_id, ping_eu, disk_usage,insert_time from sequencial_host;
  +---------+---------+-------------+---------------------+
  | host_id | ping    | disk_usage  | insert_time         |
  +---------+---------+-------------+---------------------+
  |       1 |   35.60 | 10329416704 | 2016-01-20 20:47:51 |
  |       2 |   36.57 |  2902848512 | 2016-01-20 20:48:06 |
  +---------+---------+-------------+---------------------+
  </code></pre>
  
  <p>I want to prevent a mess in case the script runs amok or any malicious intent by calling a stored procedure that allows inserting if the last insert_time was at least 4,5 Minutes ago. (30 second script built in time out, so more a less accurate).
  I built this Procedure:</p>
  
  <pre><code>CREATE PROCEDURE `update_table`(
  IN host_id_a int(11),
  IN insert_time timestamp,
  IN disk_usage bigint(20) ,
  IN ping decimal(10,2)
  BEGIN
  DECLARE count INT;
  SELECT count(insert_time) INTO count FROM db.sequencial_host WHERE insert_time &gt; (NOW() - INTERVAL 270 SECOND) AND host_id = host_id_a ORDER BY insert_time DESC LIMIT 1;
  
  IF count &lt; 1 THEN 
      INSERT INTO db.sequencial_host ( `host_id`,`insert_time`,`disk_usage`,`ping`) 
      VALUES( host_id_a, CURRENT_TIMESTAMP, disk_usage, ping);
  END IF;
  END
  </code></pre>
  
  <p>If I leave away the If clause, the Insert Statements work by calling the procedure and the Select Query also works for itself if executed in the shell.
  I am using MariaDB 5.5.46-1ubuntu0.14.04.2.</p>
  
  <p>I nearly tried any combination of IF count = 0, IF count IS NULL, IF (SELECT ..) IS NULL.</p>
  
  <p>Could anybody take look?
  With kind regards Joey</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>MariaDB [tempdb]&gt; SELECT count(*) FROM tempdb.sequencial_host WHERE insert_time &gt; (NOW() - INTERVAL 270 SECOND) AND host_id = 1 ORDER BY insert_time DESC LIMIT 1;
  +--------------------+
  | count(insert_time) |
  +--------------------+
  |                  0 |
  +--------------------+
  1 row in set (0.01 sec)
  
  MariaDB [tempdb]&gt; SELECT count(*) FROM tempdb.sequencial_host WHERE insert_time &gt; (NOW() - INTERVAL 2 HOUR) AND host_id = 1 ORDER BY insert_time DESC LIMIT 1;
  +--------------------+
  | count(insert_time) |
  +--------------------+
  |                  1 |
  +--------------------+
  </code></pre>
  
  <p><strong>Edit 2:</strong>
  Pastebin containing all relevant database information in complete. <a href="http://pastebin.com/99eEDLy4" rel="nofollow">pastebin.com/99eEDLy4</a> </p>
  
  <p><strong>Edit 3:</strong>
  If I change it to cnt = 1 I can spam the query and it fills everything, but only if there is data in the table. With a freshly truncated one I need to set it to 0 to get values in there.  </p>