Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite get max id not working (?)

Tags:

sql

select

sqlite

Im using this:

SELECT * 
WHERE id=MAX(id) 
FROM history;

But my query is empty. I have also tried this (This one works):

SELECT MAX(id) AS max_id 
FROM history;

But obviusly my query only contains the max_id key. What am I doing wrong with the first one?

like image 940
Ediolot Avatar asked Mar 16 '15 20:03

Ediolot


1 Answers

You need to add another level of select for the MAX, like this:

SELECT * 
WHERE id=(SELECT MAX(id) from history)
FROM history;

A better approach would be to order by id in descending order, and limit the output to a single row:

SELECT *
FROM history
ORDER BY id DESC
LIMIT 1
like image 126
Sergey Kalinichenko Avatar answered Nov 07 '22 13:11

Sergey Kalinichenko