Sqlite get max id not working (?)

流过昼夜 提交于 2020-07-17 11:27:05

问题


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?


回答1:


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



回答2:


SELECT top 1 FROM history ORDER BY id DESC




回答3:


Actually most simple query to get the max value of a column in sqlite is:

select MAX(id) from table;


来源:https://stackoverflow.com/questions/29086705/sqlite-get-max-id-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!