How to get the last integer ID you query in MySQL

334 views Asked by At

I am trying to get the last ID input that I query in MySQL Database.

Note: The itemid is auto increment and of type integer.

In this example, I want to get the last item query, which is itemcode 13.

Here is the screenshot of the table:

enter image description here

3

There are 3 answers

0
Sekretoz On

try this

rs = st.executeQuery("select last_insert_id() as last_id from table limit 0,1");
    lastid = rs.getString("last_id");
0
Frankely Diaz On

You can do this, the nested queries retrieves the maximum itemcode and then filter the main query in order to fetch the row.

select * from table_name where itemcode = (select max(itemcode) from table_name);
0
Zaw Than oo On

I think, you are trying to retrieve the maximum value of itemcode. Use MAX operator. result will be 13.

select max(itemcode) from table_name.