-1

I am currently working on a PHP Forum project. And now, I want to do pagination in my thread lists.

So, I want to know that is there a way to add auto-increment in SQL such that first ten inserts will have an Id of 1 and next ten will have an Id of 2 and so on...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rishabh
  • 23
  • 3
  • Nope - you will have to code this yourself and BTW what do you mean by FIRST 10? Is that first 10 inserts? – P.Salmon Sep 07 '20 at 09:57
  • yes. I mean inserts – Rishabh Sep 07 '20 at 10:00
  • @Rishabh Why do you want to cache the page number? Why not use a normal `LIMIT x,y` expression in your `SELECT` query? – Progman Sep 07 '20 at 10:09
  • I do not see the reason in CREATION of the column with such properties. Create common autoincremented field (`id`), and calculate the number which you need in the queries using according expression: `SELECT (id + 9) MOD 10 AS id, ...` – Akina Sep 07 '20 at 10:22
  • 2
    Your question is regarding "Your" approach to pagination. Do you think your approach to pagination would work if later I want to say delete 7th record? In that case, you would need to edit the page_number field all records from 7th to last. Pagination is a read side problem. Solving it during insert is not a correct solution. Please go through different Pagination strategies in this StackOverflow question - https://stackoverflow.com/questions/2616697/php-mysql-pagination – faizan Sep 07 '20 at 13:30

1 Answers1

2

Auto increments doesn't work that way. but if you really need to group your inserts with some id, you can use triggers. you can create an insert trigger and write your logic to set the field as per your requirement.

Vilsad P P
  • 1,529
  • 14
  • 23