Hello. There is a certain database in which you need to create a new line.

Now I do it using two queries, the first one creates, and the second one extracts:

INSERT INTO positions (title) VALUES ('title') SELECT id FROM positions WHERE title='title' 

But this, of course, is wrong, since in the case of two identical title will produce two id .

How to make one request which would create a line, and then return its id ?

    1 answer 1

    See LAST_INSERT_ID() . There is also a function for php

    • But this will be the last one added to the entire database !? - Stanislav Dalinin
    • In the table, and then if the id is AUTO_INCREMENT. - Ale_x
    • I figured it out, thanks) - Stanislav Dalinin
    • one
      not at all) There are still some features of this function - in case of multiple insertion, if id is AUTO_INCREMENT, but you rigidly insert the field value. In general, google - will not be superfluous. - Ale_x