So we were creating a row, obtaining the generated auto-increment value, deleting the row to keep the table clean, and forwarding the value to the payment site. So we had to make sure the ID the third party got and sent back to us was unique and would stay that way (and of course there's the possibility the user would cancel the transaction after they had been redirected). This was a problem for us, as we were using the table and mysql's auto-increment feature to neatly manage IDs in a multi-threaded environment where users were getting re-directed to a third-party payment site. If there are no rows, it will start at 1. So when you first insert a row, it finds the max value of the field that uses auto-increment, adds 1 to this value, and uses the resulting value. So in plain English, after the MySQL service starts it has no idea what the auto-increment value for your table should be. If the table is empty, InnoDB uses the value 1. InnoDB increments by one the value retrieved by the statement andĪssigns it to the column and to the auto-increment counter for the This is a simple PHP code.The auto-increment counter is stored only in main memory, not on disk.īecause of this when the service (or server) restarts the following will happen:Īfter a server startup, for the first insert into a table t, InnoDBĮxecutes the equivalent of this statement: SELECT MAX(ai_col) FROM t SELECT user_id, name, (SELECT COUNT(t2.user_id) FROM your_table t2 WHERE t2.user_id=t1.user_id) AS increment FROM your_table t1 SELECT user_id, name, COUNT(user_id) AS increment FROM your_table GROUP BY user_id īut this will return a single value for one user. simple COUNT(), and GROUP BY would do the trick.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |