I have a Dynamic table like this
Product_id | Product_name |
---|---|
1 | Mobile Phone |
5 | Computer |
3 | Mobile Phone |
I need a query to find the last record(latest record) in a table.
I have a Dynamic table like this
Product_id | Product_name |
---|---|
1 | Mobile Phone |
5 | Computer |
3 | Mobile Phone |
I need a query to find the last record(latest record) in a table.
If your ProductID column is always incrementing, and the last record is the one with the maximum product ID then you can use this:
SELECT * FROM tablename
ORDER BY Product_ID DESC
LIMIT 1
You would probably want to create an index on your table which sorts the table according to what you want for best performance and easy querying. If the latest added item in your table is getting the highest ID, then you want to create an index on the ID's. Fx:
CREATE INDEX index_name ON table_name (Product_id)
When you have a table and your index, then you would want to write something like
SELECT Product_name FROM table_name DESC LIMIT 1;
in order to get the latest added item.
You can store the last insert id in a variable and insert in to another table
SET @last_id_in_table1 = LAST_INSERT_ID();
INSERT INTO table2 (parentid,otherid) VALUES (@last_id_in_table1,1);