Greetings. How can I generate a value for the user id field (a varchar type field) using the pattern 000001, 000002, etc.? That is, when registering a new user, it is necessary for him to generate a userid based on the last userid in the table. Let's say if the last userid is 000012, then the next registered user should get the number 000013. Unfortunately, it is not strong in SQL and PHP, but it is necessary with the use of these technologies and preferably with PDO, but this is not critical.

  • And why not use auto-increment? Why some bikes with varchara and zeros? - Opalosolo
  • @ ua6xh Unfortunately, it is not strong in SQL. Otherwise, theoretically, judging by the leading zeros, char = would be enough) - free_ze
  • > Unfortunately, it is not strong in SQL and PHP, but who needs it with the use of these technologies? - zb '
  • one
    If you are not quick-witted, it is necessary for the one who asked the question, otherwise he would not ask it, right? - Lucky
  • one
    @Lucky, you’re a little unsure if you’re not good at PHP and SQL, how can you know that you need these technologies to solve this problem? That's why I asked "who needs?" because, given the above, the questioner may not need it. - zb '

2 answers 2

The following number can be found, for example, like this:

select right(concat('00000000000000000',max(id)+1),length(max(id))) from t; 
  • Do not learn nonsense) is auto-increment). And then to sit down you need to run to the factory and ask to make a chair! Secondly, in this case, you need a table lock. - Victor Halauko
  • I gave the exact answer to the question. And you are trying to learn. - msi
  • The purpose of this site is still a little more than just answering the questions asked. - etki
  • one
    If my answer does not meet the goals of the site owners, I am ready to incur the most severe punishment. - msi
  • one
    @Lucky, are you sure that all requests using this mechanism will always be executed sequentially ? If not, then you still have to develop a mechanism for locking table sharing. This can lead to a terrible decrease in performance. - avp

In the user table, create a field userid (int) with auto_increment. Read more here.

The field with auto_increment when adding a new record to the table automatically substitutes the number +1 of the maximum in this field.

  • @terantul, Try to write more detailed answers. Explain what your statement is based on. - Nicolas Chabanovsky
  • auto_increment will not work. As I understand it, it adds +1 on the basis of some cache, since with id auto-increment, even if you delete all the data from the table, it already starts not from 1, but from the last one, even if there are no records in the table. - Lucky
  • 3
    @Lucky * I don’t think that the planet will collapse because of such an ID * auto increment is reset to any value without any problems * errors will get from the busy key at the first concurrency race. It’s still easier to transfer this DB work. - etki
  • @Fike says right. auto increment without a problem is reset to any value. TRUNCATE your_table - clear the table and reset to 0b ALTER TABLE your_table AUTO_INCREMENT = X Where X is the number you want to start from. - Victor Halauko
  • five
    > auto-increment stores in int, but it doesn’t have leading zeros, and I still need to export to pdf to show this list to people. It never occurred to you that the data from the database do not have to be displayed in pdf one-on-one? What can and should be formatted and transformed into the required form? The database is the essence of a different order, it should not be tailored to the needs of the display. It's your business, of course, but I would send an employee for technical support to rake letters from the Indians for such a “solution” - DreamChild