title: How to delete rows from grouped data in MySQL
body: <p>I have a table:</p>
  
  <p><img src="https://i.stack.imgur.com/wlHI6.jpg" alt="enter image description here"></p>
  
  <p>I want to leave a maximum of two entries per <strong>live_login</strong>, which records sorted by <strong>last_login_ts</strong>.</p>
  
  <p>As a result, you should get:</p>
  
  <p><img src="https://i.stack.imgur.com/M5dC4.png" alt="enter image description here"></p>
  
  <p><strong>!</strong> If is it possible, than using only SQL, without php and other tools.</p>
  
  <p><strong>P.S.</strong> Sorry for my english :)</p>
  
  <p><strong>ANSWER</strong>:</p>
  
  <pre><code>delete gr.* from my_table gr inner join (Select x.* from my_table x join my_table y on y.live_login = x.live_login and y.last_login_ts &lt;= x.last_login_ts group by x.live_login, x.last_login_ts having count(*) &gt; 2) gh on gh.live_login=gr.live_login and gh.dead_login=gr.dead_login;
  </code></pre>
  
  <p>Thank you, <strong>Strawberry</strong>!</p>