Home > Mobile >  Why offset. limit returns all rows?
Why offset. limit returns all rows?

Time:05-04

The following SQL query return all rowsa instead from 12 to 18.

SELECT `reviews`.*, 
       `users`.`username` as `user_username`, 
       `users`.`slug` as `user_slug` 
FROM `reviews` 
JOIN `users` ON `users`.`id` = `reviews`.`user_id` 
WHERE `reviews`.`product_id` = 2 
ORDER BY `reviews`.`created_at` DESC LIMIT 12, 18

As result I have to get 6 rows instead all.

CodePudding user response:

Using the OFF SET in the LIMIT query

The OFF SET value is also most often used together with the LIMIT keyword. The OFF SET value allows us to specify which row to start from retrieving data

Let’s suppose that we want to get a limited number of members starting from the middle of the rows, we can use the LIMIT keyword together with the offset value to achieve that. The script shown below gets data starting the second row and limits the results to 2.

SELECT * FROM `members` LIMIT 1, 2;

Refrence and MySQL docs

In your case to return only 6 rows starting from 12(thirteen row) it should be ORDER BY reviews.created_at DESC LIMIT 12, 6

  • Related