how to get a specific id within 5 rows in a paging query in Mysql

Mosaab Alkhatib

I have a function in php I use it for paging it is like this :

$query = "SELECT id, 
      FROM table
      ORDER BY id ASC
      LIMIT $offset,5";

this work fine but what I want is to get the page that contain id number let say 10 and with it the other 4 rows, I want it to return something like this:

  • 7,8,9,10,11,12 -> if I give it id number 10.
  • 25,26,27,28,29 -> if I give it id number 26 and so on.

like it would return the 5 rows but I want to know how to set the offset that will get me the page that have the 5 rows with the specified id included.

what should I do like adding where clause or something to get what I want!

Piotr

Notice that the IDs in your table won't be consecutive if you delete some rows. The code below should work in such conditions:

$result = mysql_query('select count(*) from table where id < ' . $someId);
$offset = mysql_result($result, 0, 0);

$result = mysql_query('select * from table order by id limit ' . max($offset - 2, 0) . ',5');
while ($row = mysql_fetch_assoc($result)) {
    print_r($row);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to select rows with specific condition within specific ranage in MySQL?

From Dev

How to select rows with specific condition within specific ranage in MySQL?

From Dev

How to perform MySQL query to get all the rows from different subscriptions sorted by id?

From Dev

How to get this specific user rankings query in mysql?

From Dev

mySQL search query to get user id's from multiple rows

From Dev

php-mysqli - In sql query if search for ID, how to get also 5 row above and 5 rows below this result

From Dev

How to query rows between specific date range in MySQL efficiently?

From Dev

How to show the rows of this specific mySQL table with a single query and PHP?

From Dev

how to update all rows based on specific date by a single mysql query

From Dev

How to get URL ID into MySQL query for CodeIgniter

From Dev

Query a specific name within the same table (MySQL)

From Dev

How do I get the IDs of everybody with specific rows/values in MySQL

From Dev

How to get number of rows by select query using mysql

From Dev

How do I get the rows affected by a MySQL query in PHP?

From Dev

How do I query all rows within a 5-mile radius of my coordinates?

From Dev

How do I query all rows within a 5-mile radius of my coordinates?

From Dev

MySQL query for last rows that have a specific field

From Dev

Mysql Query for counting rows with specific days difference

From Dev

How can I select multiple rows, all having a same id (or any variable) within a database using MySQL?

From Dev

How get data from MySQL database by specific id in url

From Dev

How to get specific rows in Laravel?

From Dev

How to hide rows of a query of MySQL?

From Dev

How to hide rows of a query of MySQL?

From Dev

For a specific user_id get the latest rows (time-wise) that this user is in with all other users, in MySQL

From Dev

How to query a specific document by id

From Dev

How to get current and previous ids of current id in one query(mysql)

From Dev

How to get parent_id using mysql query?

From Dev

MySQL 5.7 - how to get id for a max value and group by query?

From Dev

TSQL query to return all rows that are within 5 mins of each other

Related Related

  1. 1

    How to select rows with specific condition within specific ranage in MySQL?

  2. 2

    How to select rows with specific condition within specific ranage in MySQL?

  3. 3

    How to perform MySQL query to get all the rows from different subscriptions sorted by id?

  4. 4

    How to get this specific user rankings query in mysql?

  5. 5

    mySQL search query to get user id's from multiple rows

  6. 6

    php-mysqli - In sql query if search for ID, how to get also 5 row above and 5 rows below this result

  7. 7

    How to query rows between specific date range in MySQL efficiently?

  8. 8

    How to show the rows of this specific mySQL table with a single query and PHP?

  9. 9

    how to update all rows based on specific date by a single mysql query

  10. 10

    How to get URL ID into MySQL query for CodeIgniter

  11. 11

    Query a specific name within the same table (MySQL)

  12. 12

    How do I get the IDs of everybody with specific rows/values in MySQL

  13. 13

    How to get number of rows by select query using mysql

  14. 14

    How do I get the rows affected by a MySQL query in PHP?

  15. 15

    How do I query all rows within a 5-mile radius of my coordinates?

  16. 16

    How do I query all rows within a 5-mile radius of my coordinates?

  17. 17

    MySQL query for last rows that have a specific field

  18. 18

    Mysql Query for counting rows with specific days difference

  19. 19

    How can I select multiple rows, all having a same id (or any variable) within a database using MySQL?

  20. 20

    How get data from MySQL database by specific id in url

  21. 21

    How to get specific rows in Laravel?

  22. 22

    How to hide rows of a query of MySQL?

  23. 23

    How to hide rows of a query of MySQL?

  24. 24

    For a specific user_id get the latest rows (time-wise) that this user is in with all other users, in MySQL

  25. 25

    How to query a specific document by id

  26. 26

    How to get current and previous ids of current id in one query(mysql)

  27. 27

    How to get parent_id using mysql query?

  28. 28

    MySQL 5.7 - how to get id for a max value and group by query?

  29. 29

    TSQL query to return all rows that are within 5 mins of each other

HotTag

Archive