title: MySQL How to update updated_by field only when new data is saved or inserted
body: <p>I have a frontend PHP application which will save data in a MySQL database. The <strong><code>updated_by</code></strong> value will change since it will be the user who is updating the record.<br />
  <strong>I would like to update the <code>updated_by</code> column ONLY when we are inserting new values for any of the other columns (<code>sname</code>, <code>scol2</code>) , and the record is inserted or updated.</strong><br />Any help is welcome, thank you so much :)
  <br />
  <br />
  <br />
  <strong>Table Info:</strong><br /></p>
  
  <ul>
  <li><strong>Table</strong>: <code>test</code></li>
  <li><strong>Unique Index:</strong> columns <code>sname</code> and <code>scol2</code></li>
  <li><strong>Columns:</strong> <code>sname</code>, <code>scol2</code>, <code>updated_at</code>, <code>updated_by</code></li>
  <li><strong>Values:</strong> <em>name1, col2, 03-02-2013, phpuser1</em></li>
  </ul>
  
  <p><br />
  <strong>MySQL 5.6 Info:</strong> <br />
  <a href="http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html" rel="nofollow">INSERT INTO.. ON DUPLICATE KEY UPDATE</a>:  <br />"..returns 1 if the row is inserted as a new row, and 2 if an existing row is updated..."
  <br />
  <br />
  <br />
  <strong>Example: users will Update or Insert records:</strong> 
  <br />
  <br />
  <code>INSERT INTO test(sname) VALUES("name1") ON DUPLICATE KEY UPDATE sname="name1", scol2="col2";</code> <br />
  Result: The above query will not update anything. Perfect. <br /></p>
  
  <p><code>INSERT INTO test(sname) VALUES("name1") ON DUPLICATE KEY UPDATE sname="name1", scol2="col22";</code> <br />
  Result: The above query would update the columns: <strong><code>scol2</code></strong> and <strong><code>updated_at</code></strong> .Perfect. <br />
  <br />
  <br />
  <strong>How to automatically update the updated_by column in one query?</strong> 
  <br />
  <br />
  <code>INSERT INTO test(sname) VALUES("test1") ON DUPLICATE KEY UPDATE sname="name1", scol2="col2", updated_by="phpuser2222";</code> <br />
  This wont work, it will update everytime the columns <strong><code>updated_by</code></strong> and <strong><code>updated_at</code></strong> <br />
  <br />
  <br />
  <strong>Possible solution with two queries:</strong> </p>
  
  <ol>
  <li>Insert the record with: <code>INSERT ... ON DUPLICATE KEY UPDATE...</code> </li>
  <li>Get the return value in my PHP application to know if it has been [updated or inserted], and then update the field <strong><code>updated_by</code></strong> with a second query: <code>INSERT INTO test(updated_by) VALUES("phpuser2222");</code></li>
  </ol>