MySQL选择产品的所有组件

时间:2015-02-01 20:36:53

标签: python mysql sql select mysql-python

我在汽车行业工作。我们的产品包含产品组件。

这是我们的一个产品的树视图。产品的一部分通过装配操作制造E40。 enter image description here

这是我们MySQL数据库的表格视图:

enter image description here

我们希望获得产品所有组件的结果。像这样:

  

' E40-1',' E40-2',' E40-3',' E40-4',&#39 ; E40-5',' E40-6',' E40-6-1',   ' E40-6-2'

所以结果'长度必须是8。

我尝试了一些MySQL查询,但他们没有工作。

我尝试的查询:

select alt_bilesen from urunler_seviyeler where parcano in (select parcano from urunler_seviyeler where parcano="E40")

这只给我们产品的六个部分。否E40-6-1和否E40-6-2。 我也试过了:

select alt_bilesen from urunler_seviyeler where parcano in (select alt_bilesen from urunler_seviyeler where parcano="E40")

这只给我们产品的最低部分。 E40-6-1和E40-6-2。不是其他部分。

顺便说一句,我们可以使用Python。如果我们想使用Python,我认为我们必须使用for循环,while循环等来调用它。你能给我一个建议吗?谢谢。

3 个答案:

答案 0 :(得分:2)

MySQL不支持" hierarchy"查询。可以使用多个查询将该功能模拟到有限数量的级别。查询的结果可以与UNION ALL操作结合使用。

获取按特定顺序返回的行可能会有问题,具体取决于您返回行所需的实际条件。

第一级:

SELECT t1.alt_bilesen
  FROM urunler_seviyeler t1 
 WHERE t1.parcano = 'E40'

第二级:

SELECT t2.alt_bilesen
  FROM urunler_seviyeler t1
  JOIN urunler_seviyeler t2
    ON t2.parcano = t1.alt_bilesen
 WHERE t1.parcano = 'E40'

第三级:

SELECT t3.alt_bilesen
  FROM urunler_seviyeler t1
  JOIN urunler_seviyeler t2 ON t2.parcano = t1.alt_bilesen
  JOIN urunler_seviyeler t3 ON t3.parcano = t2.alt_bilesen
 WHERE t1.parcano = 'E40'

第四级:

SELECT t4.alt_bilesen
  FROM urunler_seviyeler t1
  JOIN urunler_seviyeler t2 ON t2.parcano = t1.alt_bilesen
  JOIN urunler_seviyeler t3 ON t3.parcano = t2.alt_bilesen
  JOIN urunler_seviyeler t4 ON t4.parcano = t3.alt_bilesen
 WHERE t1.parcano = 'E40'

可以将查询与UNION ALL集合运算符

组合在一起
( SELECT t1.alt_bilesen
    FROM urunler_seviyeler t1 
   WHERE t1.parcano = 'E40'
)
UNION ALL
( SELECT t2.alt_bilesen
    FROM urunler_seviyeler t1
    JOIN urunler_seviyeler t2
      ON t2.parcano = t1.alt_bilesen
   WHERE t1.parcano = 'E40'
)
UNION ALL
( SELECT t3.alt_bilesen
   FROM urunler_seviyeler t1
   JOIN urunler_seviyeler t2 ON t2.parcano = t1.alt_bilesen
   JOIN urunler_seviyeler t3 ON t3.parcano = t2.alt_bilesen
  WHERE t1.parcano = 'E40'
) 
UNION ALL
( SELECT t4.alt_bilesen
    FROM urunler_seviyeler t1
    JOIN urunler_seviyeler t2 ON t2.parcano = t1.alt_bilesen
    JOIN urunler_seviyeler t3 ON t3.parcano = t2.alt_bilesen
    JOIN urunler_seviyeler t4 ON t4.parcano = t3.alt_bilesen
   WHERE t1.parcano = 'E40'
)
ORDER BY 1

可以包含额外的"等级"列,通过在每个查询中包含一个文字值。要通过返回列之外的其他内容进行排序,您需要在每个查询中包含一些其他表达式...

答案 1 :(得分:1)

我建议使用部件号本身:

select alt_bilesen
from urunler_seviyeler
where CONCAT(parcano, '-') like 'E40-%'

CONCAT()阻止E400与E40混淆。

否则,您需要开始使用join s。并且,如果子部件可以深深地嵌套,则存在问题。 MySQL不支持对分层或嵌套数据结构的查询。

答案 2 :(得分:1)

这将比您的示例深3级,但也适用于级别较低的产品,如您的示例所示。如果需要,您可以添加到查询中(如果任何产品具有超过4个级别):

小提琴演示: http://sqlfiddle.com/#!2/681368/1/0

select t.parcano,
       trim(both ', ' from
            concat(group_concat(distinct t.alt_bilesen order by
                                t.alt_bilesen separator ', '),
                   ', ',
                   ifnull(concat(group_concat(distinct l1.alt_bilesen order by
                                              l1.alt_bilesen separator ', '),
                                 ', '),
                          ''),
                   ', ',
                   ifnull(concat(group_concat(distinct l2.alt_bilesen order by
                                              l2.alt_bilesen separator ', '),
                                 ', '),
                          ''),
                   ', ',
                   ifnull(concat(group_concat(distinct l3.alt_bilesen order by
                                              l3.alt_bilesen separator ', '),
                                 ', '),
                          ''),
                   ', ',
                   ifnull(concat(group_concat(distinct l4.alt_bilesen order by
                                              l4.alt_bilesen separator ', '),
                                 ', '),
                          '')

                   )) as parts
  from urunler_seviyeler t
  left join urunler_seviyeler x
    on x.alt_bilesen = t.parcano
  left join urunler_seviyeler l1
    on t.alt_bilesen = l1.parcano
  left join urunler_seviyeler l2
    on l1.alt_bilesen = l2.parcano
  left join urunler_seviyeler l3
    on l2.alt_bilesen = l3.parcano
  left join urunler_seviyeler l4
    on l3.alt_bilesen = l4.parcano
 where x.parcano is null
 group by t.parcano

要为每个部分生成一行而不是聚合列表(这是您在问题中作为预期输出放置的行),您可以创建一个视图非规范化数据:

create denorm_vw as
select t.parcano,
       t.alt_bilesen as l0,
       l1.alt_bilesen as l1,
       l2.alt_bilesen as l2,
       l3.alt_bilesen as l3,
       l4.alt_bilesen as l4
  from urunler_seviyeler t
  left join urunler_seviyeler x
    on x.alt_bilesen = t.parcano
  left join urunler_seviyeler l1
    on t.alt_bilesen = l1.parcano
  left join urunler_seviyeler l2
    on l1.alt_bilesen = l2.parcano
  left join urunler_seviyeler l3
    on l2.alt_bilesen = l3.parcano
  left join urunler_seviyeler l4
    on l3.alt_bilesen = l4.parcano
 where x.parcano is null;

然后运行:

select parcano, l0 from denorm_vw union 
select parcano, l1 from denorm_vw where l1 is not null union 
select parcano, l2 from denorm_vw where l2 is not null union 
select parcano, l3 from denorm_vw where l3 is not null union 
select parcano, l4 from denorm_vw where l4 is not null

小提琴: http://sqlfiddle.com/#!2/bd5a4/4/0