我有查询:MySQL的SELECT语句的结果限制
select id, name from categories where parent_id in (
select id
from categories
where top_main_place > 0
)
它选择儿童信息的父节点(内选)
问题的(外部选择):我并不需要有所有子节点数据,最大值子节点数据每父母id
我该如何达到这个结果?
顺便说一句,对不起,我英文不好
我有查询:MySQL的SELECT语句的结果限制
select id, name from categories where parent_id in (
select id
from categories
where top_main_place > 0
)
它选择儿童信息的父节点(内选)
问题的(外部选择):我并不需要有所有子节点数据,最大值子节点数据每父母id
我该如何达到这个结果?
顺便说一句,对不起,我英文不好
您应该由外部查询parent_id,给每一行一个行号(每当parent_id改变时重新设置行号),然后过滤出行号大于6的行,例如检查this question和sql代码。
这个页面展示如何限制查询返回的结果的数量,在SQL的几种方言:http://www.w3schools.com/sql/sql_top.asp
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;
貌似@nobody打我的建议,但这应该工作。 –
你们俩帮了很多,谢谢! :) – user973254
这是非常有用的,谢谢你,我现在正在读.. – user973254