title: How to insert new (only changed) lines from a csv file into a mysql table
body: <p>I generate reports through a web system that was built for the company I do most of my work for.  Basically the web system tracks every hour I spend doing billable work.  It generates CSV reports.</p>
  <p>I am importing these reports into my own local mysql database, so that I can generate invoices from it.  When I invoice, I'm putting two weeks worth of line-items (from the report) into the invoice.  Then I'm marking the appropriate line-items as having been invoiced for.</p>
  <p>My question is this:  every time I download the report, it's a complete report of all my work history.  It's cumbersome to edit the csv every time and remove everything I'm not interested in invoicing for, before importing that csv into my mysql database.  Especially since in many cases, it can be months before I can invoice for a particular line item.</p>
  <p>I've looked at the mysql pages for UPDATE, REPLACE ... ON DUPLICATE UPDATE, etc.  My head is about to explode and I don't understand what I'm reading.</p>
  <p>The goal is this: I want to be able to import the report today, invoice for a bunch of it and mark it as such.  Then, I want to be able to download a new report say, tomorrow, with new content on it, and have only the new content imported.  Old data shouldn't be imported since it:
  a) already exists in the database and,
  b) may have been modified explicitly in the database, for example by marking &quot;invoiced&quot; in one of the columns, etc.</p>
  <p>Umm.. help?</p>
  <pre><code>EDIT:
  Ok, so if my CSV contains the following:
  7,8,9,
  4,5,6,
  1,2,3,
  
  And I import that into my database, then my table contains:
  7,8,9,
  4,5,6,
  1,2,3,
  
  and I can make edits/changes etc to the database.
  
  I generate a new report later and my csv looks like this:
  16,17,18,
  13,14,15,
  10,11,12,
  7,8,9,
  4,5,6,
  1,2,3,
  
  
  Now, I want to bring only the changes into my DB, as in, I want to only:
  16,17,18,
  13,14,15,
  10,11,12,
  
  so that my db now looks like my csv:
  16,17,18,
  13,14,15,
  10,11,12,
  7,8,9,
  4,5,6,
  1,2,3,
  
  
  But I don't want to edit/change/touch those last 3 lines, because I may have a good reason for editing them or adding new data into them in my db (after importing them from the csv of course).
  </code></pre>
  <p>EDIT2:
  I got it to work by flipping the order of my csv rows.  New rows were being added to the top, meaning when I imported into my table, the id for the newest row was 1, which wasn't helping the solutions below work well.</p>
  <p>The code I'm using, that now works:</p>
  <pre><code>SELECT *
  FROM lineitems_temp
  LEFT OUTER JOIN lineitems
  ON lineitems_temp.id = lineitems.id
  WHERE lineitems.id IS NULL
  ORDER BY -lineitems_temp.id
  </code></pre>
  <p>(this shows line items in lineitems_temp, which is the latest report, that aren't present on lineitems, which is the old report.  I think it's possible also to use this mechanism to find lines that have been edited etc, in order to help manually keep things in sync.)</p>