title: How to limit the amount of data for each user in a MySQL table and automatically delete the oldest data
body: <p>I am making a web application where I want to save and display the 10 most recent pages viewed by the user for the user, and I can only come up with everytime a user view a page, the page will insert a data into a MySQL table called 'recentview', then I will call the data and display it as recently viewed pages in the user interface. </p>
  
  <p>Now the problem is I want to limit the number of entries for each user to 10 inside the MySQL table, and when a user visited the 11th page, the table should automatically delete the oldest data for that particular user, so there must always been max of 10 entries for each user inside the table.</p>
  
  <p>So far I have been searching through the web and most I could find are people asking how to limit the data entries for the whole table, while what I'm looking for is limiting the entries for each user.</p>
  
  <p>So far here is the PHP code :</p>
  
  <pre><code>&lt;?php
  $con = mysqli_connect('localhost','root','','my_db');
  
  $email= $_COOKIE['email'];
  
  $recentsql = "INSERT INTO recent (email, page) VALUES ('email', 'page1')";
  $recentquery = mysqli_query($con, $recentsql);
  
  $countsql = "SELECT COUNT(*) FROM recent WHERE email='$email'";
  $countquery = mysqli_query($con,$countsql);
  $countrow = mysqli_fetch_array($countquery);
  $count = $countrow[0];
  
  if ($count &gt; 10) {
    //php codes here to delete the oldest entries for this particular user($_COOKIE['email'])//
    //I could not figure out what is the code to do this task, please help//
  }
  
  ?&gt;
  </code></pre>
  
  <p>All helps and better suggestions are very much appreciated. Thx</p>
  
  <p>OK, I apologize if my question is not very clear, so here is an example of what the MySQL table looks like :
  *I classified my user id by their e-mail address</p>
  
  <pre><code>PID   email             recentpage
  1     user1@user1.com   page1
  2     user1@user1.com   some other page
  3     user1@user1.com   other page
  4     user1@user1.com   page2
  5     user1@user1.com   some more page
  6     user1@user1.com   more page
  7     user1@user1.com   pages
  8     user1@user1.com   different page
  9     user1@user1.com   still different page
  10    user1@user1.com   page in the web
  
  11    user2@user2.com   page1
  12    user2@user2.com   other page
  13    user2@user2.com   still different page
  </code></pre>
  
  <p>so what I want is set a limit for each user (i.e user1@user1.com &amp; user2@user2.com, etc) to only 10 recentpage data in the table, and if they visit another new different page, it will delete the oldest entries, in this case for user1@user.com, "page1" and insert the newest visited page in the table, hope this makes my question clearer. Thx for helps.</p>