mariadb 如何将不同层次的产品组和规格统一到最深层次?

ttvkxqim  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(72)

方案-确定所有有效类别/产品组及其所有产品的有效规格。

  • 产品有不同的规格/功能
  • 产品始终挂在产品组树中的最低层次级别。
  • 规格可以附在不同的级别上,在这种情况下,可以假设它们只附在最高(非常一般)和最低级别(非常特殊的尺寸,压力规格等)上。

由于产品是在最低层联系起来的,我认为,最简单的办法是将上层存在的所有规格合并合并到最低层的路径中。

产品组

| ID|父ID|
| --|--|
| 1 |NULL|
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 |NULL|
| 6 | 5 |
| 7 | 6 |
| 8 | 7 |

specification_to_group

| ID|产品组ID|规范ID|
| --|--|--|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 4 | 10 |
| 4 | 4 | 11 |
| 5 | 4 | 12 |
| 6 | 5 | 1 |
| 7 | 5 | 3 |
| 8 | 7 | 12 |

结果

| 产品组ID|规范ID|
| --|--|
| 4 | 1 |
| 4 | 2 |
| 4 | 10 |
| 4 | 11 |
| 4 | 12 |
| 8 | 1 |
| 8 | 3 |
| 8 | 12 |
一种方法是尝试使用帮助表,并且不那么通用。

SELECT
    lst.id AS lst,
    up1.id AS up1,
    up2.id AS up2,
    up3.id AS up3,
    up4.id AS up4
FROM
    product_groups lst 
        LEFT JOIN
    product_groups up1 ON lst.parent_id = up1.id
        LEFT JOIN
    product_groups up2 ON up1.parent_id = up2.id
        LEFT JOIN
    product_groups up3 ON up2.parent_id = up3.id
        LEFT JOIN
    product_groups up4 ON up3.parent_id = up4.id
WHERE
    lst.id NOT IN
    (SELECT DISTINCT parent_id
        FROM product_groups
        WHERE parent_id IS NOT NULL
)
        
SELECT g.lst, s.specification_id
        FROM ht_tree g
               LEFT JOIN specification_to_group s ON g.lst = s.product_group_id
               UNION SELECT g.lst, s.specification_id
        FROM ht_tree g
               LEFT JOIN specification_to_group s ON g.up1 = s.product_group_id
UNION SELECT g.lst, s.specification_id
        FROM ht_tree g
               LEFT JOIN specification_to_group s ON g.up2 = s.product_group_id
UNION SELECT g.lst, s.specification_id
        FROM ht_tree g
               LEFT JOIN specification_to_group s ON g.up3 = s.product_group_id
UNION 
SELECT g.lst, s.specification_id
        FROM ht_tree g
               LEFT JOIN specification_to_group s ON g.up4 = s.product_group_id;
;

字符串
由于没有创建表的权限,也没有其他选项来持久化已经准备好的数据,我会考虑使用CTE

sy5wg1nm

sy5wg1nm1#

这是使用递归CTE的经典机会

WITH recursive  CTE AS(
SELECT `id`, `parent_id`, ROW_NUMBER() OVER (ORDER By id) rn, 1 lvl
FROM  product_groups
WHERE `parent_id` IS NULL
  UNION ALL

  SELECT P1.id, P1.parent_id , M.rn, M.lvl+1 AS LVL
  FROM product_groups P1  
  INNER JOIN CTE M
  ON M.id = P1.parent_id),
  getmax as(
SELECT `id`, MAX(id) OVER(PARTITION BY rn) m FROM CTE
ORDER BY rn)
SELECT sp.id, g.m, sp.`specification_id`
  FROM specification_to_group sp JOIN getmax g ON sp.`product_group_id` = g.id
ORDER By sp.id

字符串
| ID| M|规范ID|
| --|--|--|
| 1 | 4 | 1 |
| 2 | 4 | 2 |
| 3 | 4 | 10 |
| 4 | 4 | 11 |
| 5 | 4 | 12 |
| 6 | 8 | 1 |
| 7 | 8 | 3 |
| 8 | 8 | 12 |
fiddle

相关问题