Kevin Markazits
Kevin Markazits

Reputation: 27

Only show row with highest ID? (SQL)

I would like a one line row on my site where that reads: Latest registered person: x

And the person x is the person with highest ID (which is auto_increment)..

How would that code look like?

SELECT * 
FROM characters 
LIMIT 1 
ORDER BY id

Upvotes: 2

Views: 385

Answers (2)

James Allardice
James Allardice

Reputation: 166021

SELECT * FROM characters ORDER BY id DESC LIMIT 1 should do it, to get the highest id first.

Upvotes: 0

Gabi Purcaru
Gabi Purcaru

Reputation: 31564

You were very close:

SELECT *
FROM characters
ORDER BY id DESC
LIMIT 1

The syntax requires the ORDER BY to come before LIMIT, and you should have added a DESC to the ORDER BY, to get the last, not the furst user.

Upvotes: 6

Related Questions