mysql一次sql查询出9个父分类,和每个父分类对应的16个二级分类
一次查询能够实现吗?下面是我写的select * from `category` 
where pid=0 or pid in (
select p.id from (select id from `category` where pid=0 order by is_recommend desc,listorder desc limit 0,9) as p
),但是接下来怎么写,用子查询能够实现吗?

解决方案 »

  1.   

    你的语句有什么问题? 不是已经可以列出了吗?   建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  2.   

    pid=0表示是顶级分类,其他的就是子分类咯,很普通的分类tableid | pid |is_recommend(推荐) |listorder(自定义排序)  | ...
    1     0           1               
    2     1           1   .....
    ?
      

  3.   

    select * from category a,category b
    where
    a.pid=b.id
    这种方向考虑一下