两个表一个文章表,字段 id    catid
1        11
2        22
3        11
11        11
12        22
13        11
21        11
22        22
23        11
31        11
32        22
33        11一个文章分类表,字段 
catid    catname
11        科技
22        教育
请问怎么写一个sql,取出所有文章分类的前5条记录.union不好用,因为不确定有多少分类.

解决方案 »

  1.   

    select * from 文章表 as a,文章分类表 ab b where a.id=b.catid limit 0,5
      

  2.   

    http://blog.chinaunix.net/u/29134/showart_465578.html
      

  3.   

    多谢杨涛,已经解决了.SELECT a.* ,c.cat_name
    FROM article a,category c
    WHERE(
    SELECT count( * ) 
    FROM article  b
    WHERE a.cat_id = b.cat_id
    AND b.id > a.id
    order by b.id desc
    ) < 5 and a.cat_id=c.cat_id ORDER BY a.id DESC