select a.Id,a.Type,a.Title,a.ShowTime,a.UserName,a.ExternalLinks,a.Cover,a.PlainText, a.IsState,count(*) as num
from (select * from news where IsState=1 and IsDeleted=0) as a
inner join (select * from news where IsState=1 and IsDeleted=0) as b ON a.Type=b.Type
where b.id>=a.id
and a.Type in (0,2) group by a.id having num<=5 order by a.type,num;