title: MySQL query to update column in mysql based on a table?
body: <p>Assume I have the following tables:</p>
  
  <p>tableA</p>
  
  <pre><code>a_name | age | country
  Jordan |  5  | Germany
  Jordan |  6  | Spain
  Molly  |  6  | Spain
  Paris  |  7  | France
  John   |  7  | Saudi Arabia
  John   |  5  | Saudi Arabia
  John   |  6  | Spain
  </code></pre>
  
  <p>tableB</p>
  
  <pre><code>id (auto increment primary key) 
    | age | country      | group_num (initially null)
  1 |  5  | Germany      |
  2 |  6  | Spain        |
  3 |  7  | France       |
  4 |  7  | Spain        |
  5 |  8  | France       |
  6 |  9  | France       |
  7 |  2  | Mexico       |
  8 |  7  | Saudi Arabia |
  9 |  5  | Saudi Arabia |
  </code></pre>
  
  <p>I want to be able to do some kind of select/update where I am able to get the following values for the "group_num" column:</p>
  
  <p>tableB</p>
  
  <pre><code>id (auto increment primary key) 
    | age | country      | group_num
  1 |  5  | Germany      | 1
  2 |  6  | Spain        | 1
  3 |  7  | France       | 1
  4 |  7  | Spain        |
  5 |  7  | France       | 2
  6 |  9  | France       | 
  7 |  2  | Mexico       |
  8 |  7  | Saudi Arabia | 1
  9 |  5  | Saudi Arabia | 1
  </code></pre>
  
  <p>group_num is assigned based on the criteria of: </p>
  
  <pre><code>1) Places person "a_name" went. 
  2) Whether other people visited that same country. (regardless of age). 
  </code></pre>
  
  <p>The reason why id's 1,2,3,8,9 all have the same groupId is because Jordan, Molly, and Paris all happen to be somehow linked because of the above two criteria. (they all went to spain) and other countries, i.e. Germany was visited by Jordan who also visited spain, so it has the same group_num. Saudi Arabia was visited by John, who also visited spain, so it has the same group_num.</p>
  
  <p><strong>is there some SQL query or queries (may or may not involve creation of other "complementary" tables to get to the desired result shown above?</strong>  (i.e. it is okay if group_num should first to be filled with auto_incrementing values like the "id", then updated later if it is necessary. (it is okay to have non-null values for the other value fields currently shown as "(empty)"</p>
  
  <p>Cursors/iteration is very slow... The following are the steps I would perform to fill out those values, very slow process using cursors, if I can get rid of this it would be great:</p>
  
  <ol>
  <li>For tableA, we see that Jordan visited Germany at age 5. (Group_Num in tableB for [5,Germany] updated to 1).</li>
  <li>Jordan visits Spain at age 6. (Group Num for [6,Spain] updated to 1 to show its the same grouping as the same guy Jordan visited Spain)</li>
  <li>Molly visits Spain at age 6 (group_num for [6,Spain] updated to 1 since even though its a different person, the same age/country pair was hit)</li>
  <li>Paris visited France at age 7 (group_num in tableB updated to 2 since she is a different person, visited a completely different country, regardless of age.</li>
  <li>John visits Saudi Arabia at age 7 (group_num for [7,Saudi Arabia] in tableB updated to 3 for age+country pair)</li>
  <li>John visits Saudi Arabia at Age 5 (group_num for [5,Saudi Arabia] in tableB updated to 3 for age+country pair since its still John)</li>
  <li>John visits Spain at age 6 (group_num for [6,Spain] is already 1.. Jordan visited there before, there may be some grouping... so group_num for all the places John visited [6, Spain], [5, Saudi Arabia], and [7,Saudi Arabia] are all updated to 1</li>
  </ol>