title: Single SQL command to get a value, then to update that value
body: <p><strong>Main question</strong></p>
  <p>Is there a SQL command where I can select a value from a table row, and within that same SQL command update that value?</p>
  <p><strong>Question in more detail</strong></p>
  <p>I'm working on an e-commerce site where I use MYSQL Database which has a <code>products</code> table that has a list of, wait for it, products.</p>
  <p>Included in this table is a <code>quantity</code> field and a <code>sold</code> field.</p>
  <p>The <code>quantity</code> indicates the total amount of that product that was made, and the <code>sold</code> is the current amount of that product that is sold/users purchased.</p>
  <p>So if the <code>sold</code> field ever matches the <code>quantity</code> I want to update the front end HTML to indicate that it is OUT OF STOCK.</p>
  <p>So when a user purchases 'x' amount of a product, I know I can do the following two SQL commands to update the <code>sold</code> field:</p>
  <p><code>SELECT sold FROM products WHERE productID = ?</code></p>
  <p>I'd back fill that <code>?</code> with the id of the product that was just purchased.</p>
  <p>After getting that <code>sold</code> value I'd add to that the amount the user just purchased, then do an UPDATE command such as:</p>
  <p><code>UPDATE products SET sold = ? WHERE productID = ?</code></p>
  <p>Is doing two different SQL Commands the best way to do this? or is it better to have this combined in a single SQL command?</p>
  <p>Also, how could I best <strong>lock</strong> this specific <code>product</code> in the table using SQL statements, so that if a user A purchases product 'xyz' at the same time user B purchases the same product there isn't a race condition for the <code>sold</code> field that I want to update?</p>
  <p>UPDATE:
  I'm using java to communicate with the MYSQL database.</p>