如何生一个至少有一个孩子的父母?

时间:2019-06-07 09:32:02

标签: mysql

我有如下类别表:

category_id  name            parent_id
1            category1       NULL
2            subcategory1    1
3            category2       NULL
4            subcategory2    1
5            subcategory4    NULL
6            subcategory3    1
7            subcategory8    NULL
8            subcategory9    5
9            subcategory5    6
10           subcategory10   6
11           subcategory13   6
12           subcategory6    4
13           subcategory7    4
14           subcategory12   4
15           subcategory17   4
16           subcategory10   4
17           subcategory19   4

我想获取所有至少有一个孩子的父母的数据,并从每个父母的前2行中获取所有孩子的数据。

下面是我到目前为止所做的努力。

SELECT c.category_id, c1.name, 
FROM categories c
LEFT JOIN categories c1 ON c1.category_id = c.category_id where count(c1.id) > 1 
limit 2;

我的预期输出:

category_id parent_id   name            parent_name 
2           1           subcategory1    category1   
4           1           subcategory2    category1
8           5           subcategory9    subcategory4
9           6           subcategory5    subcategory3
10          6           subcategory10   subcategory3
12          4           subcategory6    subcategory2
13          4           subcategory7    subcategory2

5 个答案:

答案 0 :(得分:1)

   WITH MyRowSet
    AS
    (
    SELECT NAME
          ,PARENT_ID
            ,ROW_NUMBER() OVER (PARTITION BY PARENT_ID ORDER BY PARENT_ID) AS RowNum
    FROM categories 
    )
    SELECT NAME,PARENT_ID FROM MyRowSet WHERE PARENT_ID IS NOT NULL AND RowNum <= 2

答案 1 :(得分:0)

您在这里

SELECT
    child AS category_id,
    parent AS parent_id,
    (SELECT name FROM sql01 WHERE category_id = child) as name,
    (SELECT name FROM sql01 WHERE category_id = parent) as parent_name
FROM
(
    SELECT
        min(category_id) AS child,
        parent_id AS parent
    FROM
        sql01
    WHERE
        parent_id IS NOT NULL
    GROUP BY
        parent_id
    UNION
    SELECT
        min(category_id) AS child,
        parent_id AS parent
    FROM
        sql01
    WHERE
        parent_id IS NOT NULL
        AND category_id NOT IN
        (
            SELECT
                min(category_id) AS child
            FROM
                sql01
            WHERE
                parent_id IS NOT NULL
            GROUP BY
                parent_id
        )
    GROUP BY
        parent_id
    ORDER BY
        child ASC,
        parent ASC
) as ids
;

答案 2 :(得分:0)

我认为这对您来说已经用光了。

select  c.category_id,c.parent_id,c.name     
,(select name from  categories where category_id=c.parent_id limit 1 ) as parentName
from categories c where c.parent_id in (
select distinct parent_id  from categories where parent_id IS NOT NULL);

答案 3 :(得分:0)

运行以下查询,希望您能准确获得所需的信息,并让我知道与此相同的任何问题。

// type this if ONLY_FULL_GROUP_BY is not set. if you are using latest version of mysql than no need to add this line.
SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));

set @num := 0, @type := '';
SELECT c2.id, c2.parent_id, c2.name, c1.name as parent_name,
      @num := if(@type = c2.parent_id, @num + 1, 1) as row_number,
      @type := c2.parent_id as dummy
FROM mst_category c1
left Join mst_category c2 ON c1.id = c2.parent_id
where c2.parent_id IS NOT NULL 

having row_number <= 2;

答案 4 :(得分:0)

这是根据get top n group of grouped results中发布的回复启发而解决的问题。

由于您未提供Mysql服务器的版本,因此尚不清楚某些新功能是否对您有用,因此此响应尝试模拟ROW_NUMBER()功能:

SELECT
    category_id, name, parent_id 
FROM
(
    SELECT
        name, parent_id, category_id, 
        @rn := IF(@prev = parent_id, @rn + 1, 1) AS rn,
        @prev := parent_id
    FROM categories
    JOIN (SELECT @prev := NULL, @rn := 0) AS vars
          where parent_id is not null
          ORDER BY category_id, parent_id
) AS T1
WHERE rn <= 2;

这里是original comment