In addition to the answers already indicated, this is also useful if you want to do operations on a table based on the number of rows present in that table.
those. using PHP if you want to delete all entries except the one with the largest identifier from the "myTable" table:
<?php $con = mysqli_connect("hostname", "username", "password", "database"); // Connect $totalRows = mysqli_query($con, "SELECT COUNT(*) FROM myTable"); // Get total row count $mysqli_query($con, "DELETE FROM myTable WHERE id >= 0 LIMIT ($totalRows - 1);"); // Delete ?>
This is really useful because if you have only 1 line left, you will be left with LIMIT 0 , which is what you need.
source share