100 entries. How to deduce from a DB 5 last records, thus without considering the last 20?

Ie should be removed records from 80 to 75.

  • one
    Use LIMIT for example, to specify from which record and how much to output - NTP
  • It's clear. Just how, if the number of records changes all the time, but the last 5 should always be output without taking into account the last 20. - Denis of Heaven
  • I can suggest each time to count the total number of records in the table and subtract 25 from this number. Get the value from which you want to display 5 records. - NTP
  • This option is also known. Maybe there are more convenient options. I will wait ... if there is no option, then I will leave this one. - Denis Heavenly
  • 3
    the number of records changes all the time, but the last 5 should always be output without taking into account the last 20 Sort change! back ... and take notes from the 21st to the 25th, regardless of how many there are. LIMIT 20,5 - Akina

2 answers 2

This code can count the number of rows in the database:

 $q="SELECT count(*) FROM имя_Π±Π°Π·Ρ‹"; $res=mysqli_query($mySQL, $q); $row=mysqli_fetch_row($res); $total_rows=$row[0]; 

And with such a request, get 5 records excluding the last 20

 $end = $total_rows - 25; \\ Π‘Ρ‡ΠΈΡ‚Π°Π΅ΠΌ, с ΠΊΠ°ΠΊΠΎΠΉ строки Π±ΡƒΠ΄Π΅ΠΌ Π±Ρ€Π°Ρ‚ΡŒ \\ SQL запрос с Π»ΠΈΠΌΠΈΡ‚ΠΎΠΌ "SELECT * FROM имя_Π±Π°Π·Ρ‹ ORDER BY `id` DESC LIMIT ".$end.",5" 

    I think it will go like this:

     SELECT * FROM имя_Π±Π°Π·Ρ‹ ORDER BY `id` DESC LIMIT 20, 25 

    https://ruhighload.com/post/Mysql+LIMIT