2

如何限制每个idinWHERE子句的结果?我的查询是:

SELECT name 
FROM location_areas 
WHERE parent IN ("1,2,3") 
ORDER BY popularity,name    

父母不是唯一的。

我需要为子句中的每个父母id获得10 个结果。WHERE

例如表结构是:

id   name   parent
1    name    0
2    name    1
3    name    1
4    name    80
5    name    80
6    name    80
7    name    80
8    name    1
4

2 回答 2

2

试试这个:

SELECT
   T.name,
   T.popularity,
   T.parent,
   T.rank
FROM
(
SELECT
   L.name,
   L.popularity,
   L.parent,
   @rank := IF(@parent = parent, @rank + 1, 1) rank,
   @parent := parent
FROM location_areas L,
(SELECT @rank := 1, @parent := NULL) R
) T
WHERE T.rank <= 10

编辑

SELECT T.name, T.popularity, T.parent, T.level, T.rank
FROM (
         SELECT L.name, L.popularity,
             L.parent, L.level,
             @rank := IF(@parent = parent, @rank + 1, 1) rank,
             @parent := parent
         FROM location_areas L,
             (SELECT @rank := 1, @parent := NULL) R
         WHERE L.parent IN (".$ids.")
     ) T WHERE T.rank <= 10;
于 2013-01-14T08:22:07.880 回答
0

你可以简单地做到这一点

SET @level = 0;
SET @group = '';

SELECT 
    name
FROM (
    SELECT 
        name ,
        parent
        @level := IF(@group = parent, @level+1, 1) AS level, 
        @group := parent as EGroup 
    FROM test
    WHERE parent IN ("1,2,3") 
    ORDER BY parent 
) rs
WHERE level < 11 
于 2013-01-14T09:30:52.397 回答