2017-05-04 240 views
-3

任何人都可以請幫助從組中獲得最後一條記錄。 enter image description here從組數據中獲取最後一條記錄。 MySql

+1

歡迎來到StackOverflow。 請參考[遊覽], 學習問好問題stackoverflow.com/help/how-to-ask, 做個[mcve]。一個MCVE應包含各種樣本輸入(說明所有方面)和期望的輸出。 對於SQL,MCVE包含幾行'create table ...'和'insert into table values ...'。 – Yunnosch

回答

1

我認爲你需要這樣的:

select * from t where col = 85 order by id desc limit 1 

根據您的意見,這應該讓每個組最後一個記錄:(假定id是獨一無二的,「最後一個記錄」是指記錄,最高id

select t.* from t 
inner join (select max(id) as maxid from t group by col) s 
on t.id = s.maxid 
+0

嗨,先生,我不僅有85 id。我在Group中有許多ID。所以我希望該組數據的最後一條記錄。 – Ravi

+0

select * from t group by subscription_id order by id desc limit 1此查詢可能適用於您。 – Sudhakar

+0

@Ravi - 我更新答案,請參閱。 –

0

你可以使用last()函數。

SELECT LAST(CustomerName)AS LastCustomer FROM Customers;

0

要從MySQL獲取1行,請使用'limit'關鍵字。

MySQL支持LIMIT子句來選擇有限數量的記錄,而Oracle使用ROWNUM。 語法:

SELECT column_name(s) 
FROM table_name 
WHERE condition 
LIMIT number; 

例如爲: 從你的截圖,訂閱ID是相同的多個ID的你想獲得最後一個記錄哪個ID是更大的。下面的查詢得到你的結果,按subscription_id分組,並通過id desc進行排序,並限制爲1使得僅從數據庫獲取1行。

select * from tableName group by subscription_id order by id desc limit 1 
相關問題