简体   繁体   English

如何使用SQL语法从MySQL表中选择最后一条记录

[英]How to select the last record from MySQL table using SQL syntax

I have a table with several records. 我有一张有几个记录的表。 There is an id field. 有一个ID字段。 I would like to select the record with the most recent id (ie the highest id). 我想选择具有最新ID(即最高ID)的记录。

Any ideas? 有任何想法吗?

SELECT * 
FROM table_name
ORDER BY id DESC
LIMIT 1

User order by with desc order: 用户使用desc命令排序:

select * from t
order by id desc
limit 1

You could also do something like this: 您还可以执行以下操作:

SELECT tb1.* FROM Table tb1 WHERE id = (SELECT MAX(tb2.id) FROM Table tb2);

Its useful when you want to make some joins. 当您想加入一些连接时,它很有用。

SELECT MAX("field name") AS ("primary key") FROM ("table name")

例:

SELECT MAX(brand) AS brandid FROM brand_tbl
SELECT   *
FROM     table
ORDER BY id DESC
LIMIT    0, 1

I have used the following two: 我使用了以下两个:

1 - select id from table_name where id = (select MAX(id) from table_name)
2 - select id from table_name order by id desc limit 0, 1
SELECT * FROM your_table ORDER BY id ASC LIMIT 0, 1

The ASC will return resultset in ascending order thereby leaving you with the latest or most recent record. ASC将按升序返回结果集,从而为您提供最新或最近的记录。 The DESC counterpart will do the exact opposite. DESC对应方将执行完全相反的操作。 That is, return the oldest record. 即,返回最早的记录。

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

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