There is such a moment, you need to send a message to 5 users at once in the system, and then send an email to everyone with a subject like “You have a new message ...”. To add these 5 users in the database in a cycle, as for me, bad practice, you can add Immediately by one request of the 5th, but then I can not get the ID of each. How are such moments resolved?

  • one
    If you need to know the id of each record, then insert in the loop is a normal solution. - ilyaplot

1 answer 1

Probably - to add users to one INSERT, and following it SELECT, to extract identifiers?

  • How do I get the identifiers of all users if I add all of them at once with one insert? Or do you propose to make a request to select the last 5 users? - quaresma89
  • Roughly speaking, you know the data of the users you added, which means you can do a SELECT for them. Something like "INSERT INTRO users (name) VALUES (UserNames)", "SELECT id FROM users WHERE name IN (UserNames);" - Alexander Pozharskii
  • But in general - I do not think that this is the case, because of which it is worth bothering. It will be more readable option with a cycle. - Alexander Pozharskii
  • And if you make such a moment? select * from users order by users.id desc limit 5 - quaresma89
  • And when executing the second instance of the script in parallel (unlikely, but still) - to get incorrect data. - Alexander Pozharskii