Return only the last record entered in the MySQL database [duplicate]

1

Hello everyone, I have a project but I have to make a query and return it to the "last line" or last record inserted in the database.

    
asked by anonymous 13.05.2017 / 20:09

1 answer

1

There are two ways to return the last record of a table in the mySQL database.

By the highest value of the primary key of the table or ID field, for example: SELECT MAX (ID) FROM table
Where variable ID is the primary key and table is the name of your table.

In descending order of selection, for example: SELECT ID FROM table ORDER BY ID DESC LIMIT 1 Where variable ID is the primary key and table is the name of your table.

By the last record added by the INSERT command, for example: INSERT INTO table (name) VALUES ($ name)

SELECT LAST_INSERT_ID () The commands must be executed together, otherwise LAST_INSERT_ID will return 0 as a result.

    
13.05.2017 / 20:30