2017-02-24 71 views
0

我使用名爲Page的表格處理mysql數據庫。有沒有什麼辦法可以從每個頁面中提取最新版本?需要幫助查詢數據庫以顯示最新版本

ID  Page Title  Version 
0001 Homepage  1 
0002 Homepage  2 
0003 Homepage  3 
0004 Contact   1 
0005 Contact   2 
0006 Basket   1 
0007 Basket   2 
0008 Basket   3 
0009 Basket   4 

我需要什麼:

ID  Page Title  Version 
0003 Homepage  3 
0005 Contact   2 
0009 Basket   4 

回答

1

您可以使用MAXGROUP BY,具體如下:

SELECT MAX(ID) 
     ,PageTtile 
     ,MAX(version) 
    FROM Page 
    GROUP BY PageTitle 
0

你也可以使用ROW_NUMBER()如下

SELECT * FROM (
SELECT Row_Number() over (partition by Title order by Version DESC) As rn, Page, Title, Version FROM Page) a 
where rn = 1 
+0

感謝您的建議都非常有幫助:) – Punkrock760

0

嘗試此查詢 -

SELECT t1.* FROM tbl t1 
JOIN (SELECT PageTitle, MAX(Version) Version FROM tbl GROUP BY PageTitle) t2 
    ON t1.PageTitle = t2.PageTitle AND t1.Version = t2.Version; 

輸出:

3 Homepage 3 
5 Contact 2 
9 Basket 4