title: How to reliably count preceding rows in MySQL?
body: <p>I would like to know how to count preceding number of rows of a given row, in a given order by clause, in MySQL.
  Also the rows may be inserted randomly, so auto_increment is unreliable.</p>
  
  <pre>
  MyAgenda - List
  
  ID   FROM                  TO
  32   2017-09-26 12:35:00   2017-09-26 13:35:00
  33   2017-10-10 12:35:00   2017-10-10 13:35:00
  32   2017-10-17 12:35:00   2017-10-17 13:35:00
  32   2017-10-24 12:35:00   2017-10-24 13:35:00
  </pre>
  
  <p>Like in this case, The rows are sorted by the "From" column, but apparently row 34 is inserted before row 36, but after sorting 36 is above 34, and if another row 37 is inserted it maybe above or below any row, or even at the top. So how can I reliably count the preceding number of rows above a given row, in a given order by clause?</p>
  
  <p>Tried the subquery method but it is O(n^2) and will be painfully slow when the number of rows is large.</p>