[英]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.