Suppose i have a variable ($var
) that determines what rows get selected for an mysql query.The table to be searched has a date column along with some others.
$var = 1
then retrieve top 5 rows in desc date order
.$var = 2
then retrieve rows 6-10 in desc date order
.$var = 3
then retrieve rows 11-15 in desc date order
.SELECT
*
FROM
my_table
ORDER BY
my_table.date DESC
LIMIT
[($var - 1) * 5], 5
Where []
is where you should embed your PHP using .
to concatenate strings if $var is between 1 and 3
This is a simple as using the OFFSET
syntax in the mysql SELECT statement:
SELECT * FROM myTable ORDER BY date DESC LIMIT ($var*5, ($var-1)*5 +1)
select * from table order by date desc limit (($var-1)*5+1), ($var*5)