分层配置选择

时间:2012-01-10 22:45:45

标签: mysql sql select configuration hierarchy

编辑:抱歉让您感到困惑。刚从我的老板那里得到了OK,发布了这部分架构。如果我被允许发布图片,我会在原帖中有更多细节。

我有一个如下所示的配置架构:

http://img717.imageshack.us/img717/7297/heirarchy.png

每个级别都包含在它下面的级别中(即 - 一个伙伴有多个程序),每个配置级别与其他类型的配置级别共享配置密钥(即 - 可以在伙伴处设置默认时区级别,然后从程序,组合或设备级别覆盖。)

这允许我们做的是对一种类型的对象使用默认值,然后使用更具体的分类法覆盖它。例如:

假设我有一个公司的合作伙伴对象。假设hierarchy_configuration_key 1是默认时区。我提出了一个partner_configuration,通常说,该合作伙伴将位于东海岸(纽约市时间)。

现在我有多个合作伙伴支持的程序。假设特定程序基于加利福尼亚州。我提出了一个program_configuration,说明该程序中的设备是萨克拉门托时间。

现在让我们跳过投资组合,并说有人在加利福尼亚州之前注册了这个项目,但仍然是客户。我们设置了一个设备配置,说明他们现在在山区时间。

层次结构如下所示:

Level     |Timezone (hierarchy_configuration_key 1)
---------------------------------------------------
Partner   |NYC
Program   |Sacramento
Portfolio |null (defaults to most granular above it, so Sacramento)
Device    |Denver

现在我想选择按hierarchy_configuration_key_id分组的配置:

我可以使用内部联接来遍历关卡,但是我希望select给我一个像这样的结果(按hierarchy_configuration_key_id分组)作为设备的主键(device_id):

device_id |portfolio_id |program_id |partner_id |device_config |portfolio_config |program_config| partner_config
---------------------------------------------------------------------------------------------------------------------
1         |2            |1          |35         |Denver        |null             |Sacramento    | NYC

同样可以接受的是Select,它只给了我最相关的配置值,即:

device_id |portfolio_id |program_id |partner_id |config_value
-------------------------------------------------------------
1         |2            |1          |35         |Denver      

提前致谢。如果您需要进一步澄清,请与我们联系。

2 个答案:

答案 0 :(得分:0)

我认为@ EugenRieck的评论指出了唯一不起作用的部分...... - Which field tells the Miata it is a Child of Mazda?

我会略微改变结构...

ENTITY_TABLE

entity_id | parent_entity_id | entity_name
     1            NULL         Vehicle
     2             1           Car
     3             2           Mazda
     4             3           Miata
     5             1           Cycle
     6             5           Unicycle
     7             6           Broken Unicycle


PROPERTY_TABLE

entity_id | property_type | value
     1          Wheels        4
     2          Wheels        NULL
     3          Wheels        NULL
     4          Wheels        NULL
     5          Wheels        2
     6          Wheels        1
     7          Wheels        0

     (And repeated for other property types as appropriate)


-- Every entity must have the same properties as the parents
-- (otherwise you have to find the topmost parent first to know what properties exist)

-- An entity may only have 1 parent

-- The topmost parent must have a NULL parent_id

-- The bottommost parent must be no more than 3 joins away from the topmost parent

然后你可以有这样的东西......

SELECT
  entity1.id,
  property1.property_type,
  entity1.name,
  entity2.name,
  entity3.name,
  entity4.name,
  property1.value,
  property2.value,
  property3.value,
  property4.value,
  COALESCE(property1.value, property2.value, property3.value, property4.value) AS inherited_value
FROM
  entity               AS entity1
LEFT JOIN
  entity               AS entity2
    ON entity2.id = entity1.parent_id
LEFT JOIN
  entity               AS entity3
    ON entity3.id = entity2.parent_id
LEFT JOIN
  entity               AS entity4
    ON entity4.id = entity3.parent_id
INNER JOIN
  property             AS property1
    ON property1.entity_id = entity1.id
LEFT JOIN
  property             AS property2
    ON  property2.entity_id     = entity2.id
    AND property2.property_type = property1.property_type
LEFT JOIN
  property             AS property3
    ON  property3.entity_id     = entity3.id
    AND property3.property_type = property1.property_type
LEFT JOIN
  property             AS property4
    ON  property4.entity_id     = entity4.id
    AND property4.property_type = property1.property_type
WHERE
    entity1.id              = @entity_id
AND property1.property_type = @property_type

答案 1 :(得分:0)

此解决方案基于您的架构,其中@ param1是hierarchy_configuration_key_id,@ param2是所需的device_id。它使用的方法类似于Dems',尽管它是独立到达的,除了我借用COALESCE。

SELECT *,
IF(dv_key IS NOT NULL,'device',IF(pf_key IS NOT NULL,'portfolio',IF(pg_key IS NOT NULL,'program',IF(pt_key IS NOT NULL,'partner',NULL)))) AS hierarchy_level,
COALESCE(dv_key,pf_key,pg_key,pt_key) AS key_id,
COALESCE(dv_value,pf_value,pg_value,pt_value) AS value
FROM
(SELECT sim_id,
dv.device_id, pt.partner_id, pg.program_id, pf.portfolio_id,
dvc.hierarchy_configuration_key_id AS dv_key, dvc.configuration_value AS dv_value,
pfc.hierarchy_configuration_key_id AS pf_key, pfc.configuration_value AS pf_value,
pgc.hierarchy_configuration_key_id AS pg_key, pgc.configuration_value AS pg_value,
ptc.hierarchy_configuration_key_id AS pt_key, ptc.configuration_value AS pt_value
FROM device dv
LEFT JOIN portfolio pf USING(portfolio_id)
LEFT JOIN program pg USING(program_id)
LEFT JOIN partner pt USING(partner_id)
LEFT JOIN device_configuration dvc ON dv.device_id=dvc.device_id AND dvc.hierarchy_configuration_key_id=@param2 AND dvc.active='true'
LEFT JOIN portfolio_configuration pfc ON pf.portfolio_id=pfc.portfolio_id AND pfc.hierarchy_configuration_key_id=@param2 AND pfc.active='true'
LEFT JOIN program_configuration pgc ON pg.program_id=pgc.program_id AND pgc.hierarchy_configuration_key_id=@param2 AND pgc.active='true'
LEFT JOIN partner_configuration ptc ON pt.partner_id=ptc.partner_id AND ptc.hierarchy_configuration_key_id=@param2 AND ptc.active='true'
WHERE dv.device_id = @param1) hierchy;