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."
The tables are:
composites: id, name, type [type is either "Condition" or "ConditionGroup"]
composites_properties: id, composite_id, property_id
groupings: id, parent_id, child_id
properties: id, key, value
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).
The best I've come up with is:
SELECT 开发者_C百科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_2
property_3
But I'm getting the following type list (with no indication to which group the conditions belong)
Condition Name 1 property_1
Condition Name 1 property_2
Condition Name 1 property_3
Condition Name 2 property_1
Condition Name 2 property_2
Condition Name 3 property_1
Condition Name 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.
精彩评论