2010-04-17 88 views

回答

117
SELECT * 
FROM table_name 
ORDER BY id DESC 
LIMIT 1 
8

用戶爲了通過desc順序:

select * from t 
order by id desc 
limit 1 
+0

你忘了OP表示希望「的」最高ID記錄 - 應該使用限制。 – MJB 2010-04-17 17:16:17

2
SELECT * 
FROM  table 
ORDER BY id DESC 
LIMIT 0, 1 
+0

'LIMIT 1,1'等於'OFFSET 1 LIMIT 1',因此倒數第二個記錄將被返回。 – Crozin 2010-04-17 17:26:57

+0

查詢更正 – yassin 2010-04-17 18:04:29

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

例如:

SELECT MAX(brand) AS brandid FROM brand_tbl 
7

你也可以做這樣的事情:

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

當你想進行一些連接時它很有用。

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 
相關問題