select b.* from 分类表 a inner join 新闻表 b on a.id=b.cid where a.fid=1 and 2>(select count(*) from 新闻表 where cid=b.cid and id<b.id)
新闻表的cid = 分类表的id
假设新闻表ID唯一 select * from 分类表 c inner join ( select a.id,a.title,a.cid from 新闻表 a inner join 新闻表 b on a.cid=cid and a.id<=b.id group by a.id,a.title,a.cid having count(b.id)<=2) on a.cid=c.id
[征集]分组取最大N条记录方法征集,及散分....
id name fid
1 中国 0
2 北京 1
3 上海 1
4 广州 1新闻表
id title cid
1 新闻1 2
2 新闻2 2
3 新闻3 3
4 新闻4 3
5 新闻5 4
6 新闻6 4
7 新闻7 2
8 新闻8 3简单写了一下、想查询出结果:
想取出一个大分类下(如:中国)的每个小分类(有多少算多少)的前2条新闻记录
from 分类表 a inner join 新闻表 b on a.id=b.cid
where a.fid=1
and 2>(select count(*) from 新闻表 where cid=b.cid and id<b.id)
新闻表的cid = 分类表的id
select * from 分类表 c inner join (
select a.id,a.title,a.cid from 新闻表 a inner join 新闻表 b
on a.cid=cid and a.id<=b.id group by a.id,a.title,a.cid having count(b.id)<=2)
on a.cid=c.id
就是 一但新闻表的里面的 条数大于3的话则、那个分类就不显现
就是 一但新闻表的里面的小分类条数大于3的话则、那个小分类就不显现