get latest registration in MySQL

3

I have a database in MySQL and each table has its primary key that was inserted using the following structure: "INSERT INTO TABLA VALUES (UUID(),CAMPO1,CAMPO2)" , until then everything is perfect.

My question is this, how can I get the last insertion of this table, which unfortunately when doing this type of insertion there is no correlative order to use last_insert_id() , among others. It should be noted that this is the query I must perform (since it is not necessary to obtain the last record immediately after inserting it): SELECT ultimo_registro FROM tabla

PS: we must bear in mind that the tables can not be modified, the db can be touched but not modified, and if they are different pages, in one where the "new SME" is registered, and in the other where is the registration of the product offered by that SME, to make that registration I need the id of that SME

I await answers, thank you very much.

    
asked by patricio milla 13.11.2018 в 15:39
source

3 answers

2

I think the most direct and simple way to do this is by separating the operation in two steps:

1-SELECT UUID()
2-INSERT INTO TABLA (id, CAMPO1) VALUES ($uuid, VALUE1)

where $uuid is the value returned from the first step. Then, you can check the record with that id.

    
answered by 13.11.2018 в 15:56
0

Dear thank you for your comments and help, the only solution I found is to create a new field that by inserting this SME of discord, insert the current date of the creation of that, I had complicated not being able to modify the tables , but talk to my superiors and they gave me the new look to create this new field. Otherwise it was impossible to obtain the last record with an id in UUID format.

thanks for your patience and help:)

    
answered by 13.11.2018 в 17:05
-3

I think there is no way to get what you want in a table like the one you show us.

To get this query you would need either a self-incremental field (from which you can get the highest value to know the last record that was inserted) or a field type TIMESTAMP to get the record with the highest date.

It could be achieved without having to touch the structure of that table, but you would need to create an auxiliary table in which to be able to store records at the same time that you make the insert of your table and of course, this new table would have to count or with a self-incremental ID or a 'TIMESTAMP' field, so I recommend that if you can, add a field of one of these two types to your table.

    
answered by 13.11.2018 в 16:00