简体   繁体   English

如何从MYSQL数据库中的最后10条记录中获取最低ID?

[英]How to get the lowest ID from the last 10 records in a MYSQL database?

I have a simple articles table and want to select the lowest ID from the last 10 records. 我有一个简单的文章表,并希望从最近10条记录中选择最低的ID。 For example if there are 11 ids, the resulted id should be 2 and if there are 10 ids the resulted id should be 1 and so on. 例如,如果有11 id,则结果id应为2 ,如果有10 id,则结果id应为1 ,依此类推。

I tried the following query on a table with 11 ids and it's outputting 1 while it should output 2 我在一个有11 ID的表上尝试了以下查询,它输出1而它应该输出2

SELECT MIN(id) FROM kisses ORDER BY id DESC LIMIT 10

Thanks 谢谢

You can use a subquery and then use the MIN: 您可以使用子查询,然后使用MIN:

SELECT MIN(ID) 
FROM (SELECT ID
   FROM Articles
   ORDER BY ID DESC
   LIMIT 10
) t

Here is the SQL Fiddle . 这是SQL小提琴

How about this, even easier: 怎么样,更容易:

SELECT ID
   FROM Articles
   ORDER BY ID DESC
   LIMIT 9,1

More fiddle: http://sqlfiddle.com/#!2/4d835/8 更多小提琴: http ://sqlfiddle.com/#!2/ 4d835/8

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM