-1

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.

Anonymous
  • 835
  • 1
  • 5
  • 21
VijayManohar7
  • 123
  • 2
  • 11
  • 2
    You already have helpful answers, but I wanted to point out that a table is not a file. There is (in most RDBMS) no order or sequence to the data. There is no first or last unless you explicitly create that condition with a date or sequence column, and then use that (with an ORDER BY for example) in the query to ensure you get the row you require. – Turophile Nov 29 '15 at 21:20
  • Possible duplicate of [Retrieving the last record in each group](http://stackoverflow.com/questions/1313120/retrieving-the-last-record-in-each-group) – Norbert Nov 29 '15 at 21:38

3 Answers3

1

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
fthiella
  • 48,073
  • 15
  • 90
  • 106
  • @ftheilla If i use Order by it will arrange based on the Descending order 5,3,1 so it ll get only 5 but i need 3 – VijayManohar7 Nov 29 '15 at 21:08
  • 1
    @VijayManohar7 how can you identify latest record? Is there a column, or a timestamp? – fthiella Nov 29 '15 at 21:09
  • 2
    @VijayManohar7: If the `Product_ID` doesn't specify the "last" record then you're not storing which record is "last". You can only sort by the data you have. There is no inherent concept of a "last record". – David Nov 29 '15 at 21:09
  • We can find the count of number of rows and iterate till the last row inorder to find the last row @David – VijayManohar7 Nov 29 '15 at 21:17
  • 1
    @VijayManohar7: The ordering of rows returned by MySQL (or any relational database I've used, really) is *not* guaranteed unless explicitly defined by an `ORDER BY` clause. Your proposed solution is both horribly inefficient (selecting *every row* when you only want *one row*) and isn't guaranteed to work (it *might* work just fine, even for a long time, until one day when it doesn't). Good luck with that. – David Nov 29 '15 at 21:19
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.

Jonas Tonny
  • 172
  • 1
  • 8
  • i dont have the highest id in last row .It is dynamic ,even the smallest element can be the last row.Is there any other way to achieve this – VijayManohar7 Nov 29 '15 at 21:31
  • 2
    You could add another column with a time stamp and sort it according to that. That's one solution, but not an optimal one. Usually if you let your DBMS handle the ID's, then the highest ID is the latest added. You can only work with the info that's in the table. So if you don't add new info, then you need to work with what you've already got. – Jonas Tonny Nov 29 '15 at 21:36
  • tanx for the suggestion ,i ll try with timestamp – VijayManohar7 Nov 29 '15 at 21:37
0

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);