I have table structure like that:
users
smf, user_id, group_id, status
With every query the value of smf should increase with a current max value + random number.
Here is the query
INSERT INTO users SELECT(SELECT MAX(smf) + FLOOR(RAND() * 15 from invoices ), 0, 0, '1''
My first question is about that how to keep concurrent integrity correct (max(id) may not give me the last row i`ve inserted, since someone else may have inserted since I did).
My second qu开发者_开发技巧estion is about how to get the value of FLOOR(RAND() * 15
without running second query ?
If your table has an auto-increment field, you can use mysq's last_insert_id to figure out what your new row is and get the new value. You could also generate the random number in the code before you insert it into the database.
精彩评论