Homeworkerrrr
Homeworkerrrr

Reputation: 1

Last Added in Table SQL

How would I query to find the last added book in my Book table.

I've tried the following but it doesn't seem to work:

SELECT BOOK_CODE AS 'Last Book Code' FROM BOOK ORDER BY BOOK_CODE DESC LIMIT 1;

Upvotes: 0

Views: 53

Answers (2)

Trent
Trent

Reputation: 86

I would use something like this:

SELECT * FROM BOOK 
WHERE BOOK_CODE = (SELECT MAX(BOOK_CODE) FROM BOOK)

Upvotes: 0

Chris Barlow
Chris Barlow

Reputation: 466

If book_code is an autoincrementid, you can use the LAST_INSERT_ID() function. This should be called right after the insert on the same connection.

SELECT LAST_INSERT_ID()

Upvotes: 1

Related Questions