我的Access数据库:  分类表(Category)结构
cid(int)     CategoryName(varchar)    ParentID(int)
 **当为根分类进ParentID=0,某分类的下级分类的ParentID为其cid  
 **分类最多两级,即只有根分类及下级分类  
产品表(Product)结构:  
productID(int)  productName(varchar)  cid(int)    level(smallint)  
 **最后的level为产品等级  
 
求Select语句:  
1) 如何用一句sql语句选取某一分类及子分类的前5条 level = 3 的产品
2) 如何用一句sql语句选取所有分类及其子分类的前5条 level = 3 的产品
(如果一句实在不行,对分类的操作和对产品的操作可以分开)谢了

解决方案 »

  1.   

    不明白楼主说清楚点啊!
    -----------------------------
    http://www.yanmingxuan.com.cn/
      

  2.   

    我的blog里面有一个方法。可以看看的。
      

  3.   

    jyk(今天由我来写的代码,明天就让程序自己完成!喜欢编程) ( ) 信誉:100    Blog  2006-12-11 11:37:22  得分: 0  
     
     
       我的blog里面有一个方法。可以看看的。
      
     
    *******************************************
    你那个只是取前两条的,没有下级或上级分类
      

  4.   

    改成 top 5 不就是前五条了嘛。某一分类及子分类的前5条 。 这个本来就不通顺吗?父级和子级怎么放在一起取前五条呢?所有分类及其子分类的前5条 同样的道理,你想想你要的记录集会是什么样子的呀。
      

  5.   

    jyk(今天由我来写的代码,明天就让程序自己完成!喜欢编程) ( ) 信誉:100    Blog  2006-12-11 13:15:27  得分: 0  
     
     
       改成 top 5 不就是前五条了嘛。某一分类及子分类的前5条 。 这个本来就不通顺吗?父级和子级怎么放在一起取前五条呢?所有分类及其子分类的前5条 同样的道理,你想想你要的记录集会是什么样子的呀。 
    ******************************************************************************某一分类及子分类的前5条 。 这个本来就不通顺吗?父级和子级怎么放在一起取前五条呢?
    所有分类及其子分类的前5条 同样的道理,你想想你要的记录集会是什么样子的呀。注明:这句话的意思,是选取某一大类及其子类下的前5个产品level=3的产品,这个没问题啊
    就是希望大类和子类(只有二级)放在一起取前5条
      

  6.   

    大类就是parentID=0的,小类就是ParentID不为0的
    最多两层,就是大类和小类
      

  7.   

    第一个:select top 5 * from [GateGory] where cid=(select cid from [Product] where level=3) or ParentID=(select cid from [Product] where level=3)     
    你试试吧,呵呵,第二个好像有点麻烦,我不是太会!
      

  8.   

    alex0917() ( ) 信誉:100    Blog  2006-12-11 15:15:55  得分: 0  
     
     
       第一个:select top 5 * from [GateGory] where cid=(select cid from [Product] where level=3) or ParentID=(select cid from [Product] where level=3)     
    你试试吧,呵呵,第二个好像有点麻烦,我不是太会!
      
    ***************************
    先谢了,这样的应该是经常用到啊,怎么会没人应呢?
      

  9.   

    1.select * from product where cid=(select cid from Category where CategoryName='某一分类')  and level=3 and rownum<=5 2.select * from product where level=3 and rownum<=5 这样不行吗?
      

  10.   

    不知道是不是这个意思2. SELECT TOP 5 * FROM [Prouduct] WHERE cid=(SELECT cid FROM [Category] WHERE ParentID = (SELECT cid FROM [Product] where level=3)) AND level=3  
      

  11.   

    kkk3279(路漫漫其修远兮..) ( ) 信誉:100    Blog  2006-12-11 18:11:47  得分: 0  
     
     
       1.select * from product where cid=(select cid from Category where CategoryName='某一分类')  and level=3 and rownum<=5 2.select * from product where level=3 and rownum<=5 这样不行吗?  
     
    ***********************************
    不是这样的
      

  12.   

    不知道是不是这个意思2. SELECT TOP 5 * FROM [Prouduct] WHERE cid=(SELECT cid FROM [Category] WHERE ParentID = (SELECT cid FROM [Product] where level=3)) AND level=3  *************
    不是这样的结果
      

  13.   

    1.select cid from Category where ParentID=02.select top 5 productid from Product where 
    cid in (select cid from Category where ParentID=1) 
    or cid=1 
    难点就是如何让2中的or前后的ParentID和cid相同,如果解决了,再套一次就好了
      

  14.   

    基础知识的问题,通常这样的表会增加两个字段:
    Depth:代表此节点的深度
    Path:代表从根节点到此节点的路径,使用节点名称不可能出现的字符作为分隔符在添加删除修改移动节点时,要维护好上述两个属性非常容易,然后却能够为你的查询带来极大的方便。你要3级节点是不是?WHERE Depth = 3,这就行了。
      

  15.   

    http://www.cnblogs.com/cathsfz/archive/2006/12/12/589464.html
      

  16.   

    分类表(Category)结构
    cid(int)     CategoryName(varchar)    ParentID(int)建议多设个字段 zhid=cast(cid as char)+cast(parentid as char)
    假如 有数据 
    001  饮料       0     0001  (zhid)
    002  可口可乐   001   001002(zhid)
    003  百事可乐   001   001003(zhid)查询 饮料类的话用(select * from category where zhid like '001%')
    这样方便多了