title: count id, insert, then update by number range in another columns in same table mysql
body: <p>I am trying to update levels in db like this:
  "count (id) from users table where referral_id = id As aggref set agg_referral = aggref AND level = 1 if aggref in between 1 to 3"
  So far i know but no success :(</p>
  
  <p><strong>Here is my db structure
  users table:</strong></p>
  
  <pre><code>  id (AI)      name       Referral id      agg_referral   Level
  =========== ========== ================== ============== ======
       1        user A          0                 0          0
       2        user B          3                 0          0
       3        user C          1                 0          0
       4        user D          3                 0          0
  </code></pre>
  
  <p><strong>Here is my code i am running it using cron job:</strong></p>
  
  <pre><code>  &lt;?php
      include( $_SERVER['DOCUMENT_ROOT'] . '/config.php' );
      mysql_connect(DB_HOST,DB_USER,DB_PASS);
      mysql_select_db(DB_NAME);
      $query="UPDATE users SET agg_referral = (SELECT COUNT(id) from users WHERE users.referral_id = users.id)";
      mysql_query($query);
  
      ?&gt;
  </code></pre>
  
  <blockquote>
    <p>I am expecting results like this</p>
  </blockquote>
  
  <pre><code> id (AI)      name       Referral id      agg_referral   Level
  =========== ========== ================== ============== ======
       1        user A          0                 0          0
       2        user B          3                 0          0
       3        user C          1                 2          1
       4        user D          3                 0          0
  </code></pre>