简体   繁体   English

使用复合设计模式对数据库进行mysql查询

[英]mysql query on database using composite design pattern

I'm not an SQL expert and therefore am having trouble wrapping my head around designing a mysql query to query database tables designed using the "composite design pattern." 我不是SQL专家,因此在设计mysql查询以查询使用“复合设计模式”设计的数据库表时遇到麻烦。

The tables are: 这些表是:

composites: id, name, type [type is either "Condition" or "ConditionGroup"] 复合材料:id,名称,类型[类型为“条件”或“条件组”]

composites_properties: id, composite_id, property_id Composites_properties:id,composite_id,property_id

groupings: id, parent_id, child_id 分组:id,parent_id,child_id

properties: id, key, value 属性:id,键,值

What I want to do is generate a query that will return the unique properties of the group's ("ConditionGroup") member conditions ("Condition") such that I end up with a Group Name and a list of Property Keys (inherited from the member conditions). 我想做的是生成一个查询,该查询将返回组(“ ConditionGroup”)成员条件(“ Condition”)的唯一属性,这样我最终得到一个组名和一个属性键列表(从成员继承)条件)。

The best I've come up with is: 我想出的最好的是:

SELECT DISTINCT properties.`key`, composites.name  
FROM composites, composites_properties, properties  
WHERE composites.id=composites_properties.composite_id
AND properties.id=composites_properties.property_id  
AND composites.id IN (
    SELECT child_id FROM groupings WHERE parent_id IN
        (SELECT id FROM composites WHERE type='ConditionGroup')
    )

This yields each member condition along with its list of properties where the properties are repeated if more than one member condition has that property. 这将产生每个成员条件及其属性列表,如果一个以上成员条件具有该属性,则重复该属性。

In the end I'd like: 最后,我想:

Group Name 组名

  • property_1 property_1

  • property_2 property_2

  • property_3 property_3

But I'm getting the following type list (with no indication to which group the conditions belong) 但是我得到以下类型列表(没有指示条件属于哪个组)

Condition Name 1 property_1 条件名称1 property_1

Condition Name 1 property_2 条件名称1 property_2

Condition Name 1 property_3 条件名称1 property_3

Condition Name 2 property_1 条件名称2 property_1

Condition Name 2 property_2 条件名称2 property_2

Condition Name 3 property_1 条件名称3 property_1

Condition Name 3 property_2 条件名称3 property_2

Any suggestions? 有什么建议么?

To be honest, I'm a little unclear on exactly what you are trying to accomplish, but I'll give it a shot. 老实说,我不清楚您要完成的目标,但我会给您一个机会。 If you can clarify how this query does not do what you want, I can probably help further. 如果您可以弄清楚该查询无法满足您的要求,那么我可能会进一步提供帮助。

SELECT      c.name, p.key
FROM        composites c
INNER JOIN  groupings g ON c.id = g.parent_id
INNER JOIN  composites_properties cp ON cp.composite_id = g.child_id
INNER JOIN  properties p ON p.id = cp.property_id
WHERE       c.type = 'ConditionGroup'

This doesn't seem like the best query in the world to me because it ignores what the children in each grouping actually are, so I'm not sure this is what you want. 对我来说,这似乎不是世界上最好的查询,因为它忽略了每个分组中的子代实际是什么,因此我不确定这是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM