I want to fetch number of rows with highest number of multiple of 20, Like if my table have 148 rows then limit should be 140 leaving the latest 8 entry behind, or if my table have 170 rows then limit will be 160. What will be the query in this case.
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password,
$db_name);
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$number= $_POST['number'];
$sql1 = "SELECT * FROM abc_table LIMIT WHAT TO ENTER HERE ";
2
Answers
As far as I know, what follows
LIMIT
has to be an integer literal.LIMIT
won’t even take something like6/2
, which would evaluate to an integer literal. I recommend just reading in the entire table, and then only processing how many rows you need in PHP.The above
while
loop should exit after reading the greatest number of multiples of 20 available. This approach is not too wasteful, since at most you would be reading in 19 extra rows from MySQL which you would end up not using.You can use variables for this:
The
?
is for the column used to specify the ordering, presumably something likeid asc
.In MySQL 8+, you would use window functions: