1. 程式人生 > >mysql 分組取得 每組記錄數

mysql 分組取得 每組記錄數

SELECT
*
FROM
film AS a
WHERE 2>
(
SELECT count(*) FROM
film
WHERE  filmTemplateId <> '' and filmTemplateId=a.filmTemplateId and addTime>a.addTime

) order by filmTemplateId

取得每組最新的兩條記錄

獲取分組後取某欄位最大一條記錄
方法一:(效率最高)
select * from test as a 
where typeindex = (select max(b.typeindex) 
from test as b 


where a.type = b.type );