2011-10-05 111 views
0

我有查询:MySQL的SELECT语句的结果限制

select id, name from categories where parent_id in (
    select id 
    from categories 
    where top_main_place > 0 
) 

它选择儿童信息的父节点(内选)

问题的(外部选择):我并不需要有所有子节点数据,最大值子节点数据每父母id

我该如何达到这个结果?

顺便说一句,对不起,我英文不好

回答

2

您应该由外部查询parent_id,给每一行一个行号(每当parent_id改变时重新设置行号),然后过滤出行号大于6的行,例如检查this question和sql代码。

+0

这是非常有用的,谢谢你,我现在正在读.. – user973254

1
select id, name FROM (
    select id, name, IF(@parent_id=parent_id, @count:[email protected]+1, @count:=0) as count, @parent_id:=parent_id from categories, (select @count:=0, @parent_id:=0) as x where parent_id in (
     select id 
     from categories 
     where top_main_place > 0 
    ) order by parent_id 
) y where count <= 6; 
+0

貌似@nobody打我的建议,但这应该工作。 –

+0

你们俩帮了很多,谢谢! :) – user973254