假设我定义了一个mysql表,如下所示:
create table test_table(
id int(10) unsigned auto_increment primary key
/*, other attributes...*/
);
给定那个表,我想这样从中获取最后一条记录:
select * from test_table order by id desc limit 1;
它工作,但感觉有点粗略,它的复杂性是什么?
是O(log(n))因为“limit”和“order by”是在选择之后执行的吗?
有没有更好的方法从自动递增表中选择最后一条记录?
最佳答案
使用这种方法也可以获得所需的输出。
SELECT * FROM test_table where id=(select max(id) from test_table);
希望,这对你有帮助。
关于mysql - MySQL Select and Limit计算复杂度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42871211/