Hello! How to write a query to the database MYSQL, to return the last generated ID from a particular table? I tried to use the mysql_insert_id() function, but as a result, for some reason, it produces not the number that I have in phpmyadmin , but a larger one. How to designate that the sample was from the desired table?
|
2 answers
If I correctly captured the essence of the question, then you can:
SELECT max(id) as maxid FROM table - I guess, yes! I asked this question earlier, only a little from the other side and received the same answer again. it happens! Thank you - cheh1
- one
SELECT id FROM table ORDER BY id DESC LIMIT 1;This query is convenient to use if the id is not integer ... - Zowie
|
Well, usually mysql_insert_id () is used in PHP and it returns the last ID you just inserted into the table, and to return the last generated ID from a particular table, you can use it here.
SELECT max (id) as maxid FROM table
And for what purpose do you use it? Usually this is necessary when parsing a document with an insert into the database, there is a hierarchical structure, multi-level, so that everything is correctly inserted into the database.
|