Can you provide an example of a query to delete the last entry in a MySQL table with a specific number of rows?

To delete the last entry in a MySQL table with a specific number of rows, you can use a combination of the ORDER BY and LIMIT clauses in your DELETE query. By ordering the rows in descending order and limiting the query to the desired number of rows, you can target the last entry for deletion.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Define the number of rows to keep
$numRowsToKeep = 10;

// Delete the last entry in the table
$sql = "DELETE FROM your_table_name ORDER BY id DESC LIMIT 1 OFFSET $numRowsToKeep";

if ($conn->query($sql) === TRUE) {
    echo "Last entry deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}

// Close the connection
$conn->close();