title: MYSQL insert into + select + CASE + delete not working
body: <p>I have 2 identical tables DATES and DATES_ARCHIVED</p>
  
  <p>I want to select in the DATES table all the rows that have dates that are older than current date -88 days, then insert those rows into the DATES_ARCHIVED.</p>
  
  <p>I have created a CASE for this.</p>
  
  <p>Then after the insertion I want to delete those same rows from the original DATES table.</p>
  
  <p>I am certain I got this to work before, but it's not working anymore it keeps giving error:</p>
  
  <p>Error Code: 1136. Column count doesn't match value count at row 1</p>
  
  <p>all the tables have the same amount of columns and I also removed the 2 triggers I had on the tables but the same error occurs.</p>
  
  <p>I am pretty sure it's because of the case where it ends AS MostRecentDate, I think this is adding a new column.</p>
  
  <p>Also if I remove all the code below the first: from dates, I get the same error</p>
  
  <p>Here is the code:</p>
  
  <pre><code>INSERT INTO dates_archived
  
  (term, course_no, date1_end, date2_end, date3_end, date4_end)
  
  SELECT
  
  term, course_no, date1_end, date2_end, date3_end, date4_end,
  
      CASE    WHEN date1_end &gt; date2_end THEN date1_end
              WHEN date2_end &gt; date1_end THEN date2_end
              WHEN date3_end &gt; date1_end THEN date3_end
              WHEN date4_end &gt; date1_end THEN date4_end
  
              WHEN date1_end &gt; date3_end THEN date1_end
              WHEN date2_end &gt; date3_end THEN date2_end
              WHEN date3_end &gt; date2_end THEN date3_end
              WHEN date4_end &gt; date2_end THEN date4_end
  
              WHEN date1_end &gt; date4_end THEN date1_end
              WHEN date2_end &gt; date4_end THEN date2_end
              WHEN date3_end &gt; date4_end THEN date3_end
              WHEN date4_end &gt; date3_end THEN date4_end
  
      ELSE 0
  
       END AS MostRecentDate
  
  FROM dates
  
  HAVING (MostRecentDate &lt;= (SELECT CURDATE() - INTERVAL 88 DAY));
  
  FROM dates
  
  WHERE (date1_end OR date2_end OR date3_end OR date4_end = MostRecentDate);
  
  DELETE FROM dates
  
  WHERE (date1_end OR date2_end OR date3_end OR date4_end = MostRecentDate); 
  </code></pre>
  
  <p>Thanks in advance,
  Chris</p>