繁体   English   中英

mysql select语句结果限制

[英]mysql select statement result limit

我有查询:

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

它选择其父节点(内部选择)的子信息(外部选择)

问题是:我不需要所有子节点数据,每个父id最多6个子节点数据

我怎样才能达到这个结果?

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

您应该通过parent_id对外部查询进行排序,为每行分配一个行号(每当parent_id更改时就重置行号),然后过滤出行号大于6的任何行,例如,请查看此问题和sql代码。

该页面显示了如何使用几种SQL方言限制查询返回的结果数: http : //www.w3schools.com/sql/sql_top.asp

select id, name FROM (
    select id, name, IF(@parent_id=parent_id, @count:=@count+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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM