title: MySQL - Comparing two dates in the WHERE clause
body: <p>I have a table, <code>links</code> that links two categories (parent &amp; child) together. The table has five fields:</p>
  <ul>
  <li>autoinc</li>
  <li>parent_category_name year(4) NOT NULL</li>
  <li>parent_category_year varchar(255) NOT NULL</li>
  <li>child_category_name  year(4) NOT NULL</li>
  <li>child_category_year  varchar(255) NOT NULL</li>
  </ul>
  <p>I am trying to write an INSERT SELECT that grabs both the parent &amp; child category_id, and inserts it into a temp table.</p>
  <pre><code>INSERT INTO temp (parent_category_id, child_category_id)
  SELECT parent.parent_category_id, child.child_category_id
    FROM links
    JOIN categories AS parent
         ON   parent.name = link.parent_category_name
         AND  parent.year = link.parent_category_year
    JOIN categories AS child
         ON   child.name = link.child_category_name
         AND  child.year = link.child_category_year
  </code></pre>
  <p>This query works fine, but I need to apply some business rules. The rules are:</p>
  <ul>
  <li>The parent year must be the same as the child year</li>
  </ul>
  <p><strong>OR</strong></p>
  <ul>
  <li>The parent year must be one year less than the child year</li>
  </ul>
  <p>I've added a WHERE clause to my query:</p>
  <pre><code>WHERE link.child_category_year = link.parent_category_year
     OR link.child_category_year - link.parent_category_year = 1 
  </code></pre>
  <p>When this INSERT statement executes in my Perl code, I get the following exception:</p>
  <pre><code>DBI Exception: DBD::mysql::db do failed: BIGINT UNSIGNED value is out of range in '(`my_database`.`links`.`child_category_year` - `my_database`.`links`.`parent_category_year`)' [for Statement &quot;
  
  </code></pre>
  <p>So, I take it that the INSERT does not like my date subtraction in the WHERE clause. I explored using the DATEDIFF function, but I am not simply looking for a one year difference in dates, but rather one year less on the parent than the child.</p>
  <p>How can I accomplish this without the insert error?</p>