title: Nested Updated MySQL Query
body: <p>Table - <code>galleries</code></p>
  
  <pre><code>+----+----------+------------+----------+
  | id | user_id  | is_primary |  photo   |
  +----+----------+------------+----------+
  |  1 | 1        | 1          | img1.jpg |
  |  2 | 2        | 1          | img2.jpg |
  |  3 | 1        | 0          | img3.jpg |
  |  4 | 1        | 0          | img4.jpg |
  |  5 | 1        | 0          | img5.jpg |
  |  6 | 3        | 1          | img6.jpg |
  |  7 | 2        | 0          | img7.jpg |
  +----+----------+------------+----------+
  
  UPDATE galleries set is_primary=0 WHERE user_id=1
  UPDATE galleries set is_primary=1 WHERE id=4
  </code></pre>
  
  <p>There have a column name <code>is_primary</code> i need to set only 1 rows <code>is_primary=1</code> which user id <code>1</code> But there have already one row which <code>is_primary = 1 and user_id=1</code></p>
  
  <p>I want to updated <code>is_primary=1</code> which <code>id=4</code> before updated I need to set all <code>is_primary=0</code> which <code>user_id=1</code>.</p>
  
  <p>I don't want to write 2 times query for updated.</p>
  
  <p>How to write nested query for update my record?</p>