![](/img/trans.png)
[英]How to select the lastest record from a group in MySQL table using SQl syntax
[英]How to select the last record from MySQL table using SQL syntax
我有一张有几个记录的表。 有一个ID字段。 我想选择具有最新ID(即最高ID)的记录。
有任何想法吗?
SELECT *
FROM table_name
ORDER BY id DESC
LIMIT 1
用户使用desc
命令排序:
select * from t
order by id desc
limit 1
您还可以执行以下操作:
SELECT tb1.* FROM Table tb1 WHERE id = (SELECT MAX(tb2.id) FROM Table tb2);
当您想加入一些连接时,它很有用。
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
我使用了以下两个:
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
ASC
将按升序返回结果集,从而为您提供最新或最近的记录。 DESC
对应方将执行完全相反的操作。 即,返回最早的记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.