简体   繁体   English

涉及内部联接和分组依据的SQL查询

[英]SQL query involving inner join and group by

I need help with a MySQL query that will optimize my Rails 3 application. 我需要有关MySQL查询的帮助,该查询将优化我的Rails 3应用程序。

I have 2 tables: segments and custom properties. 我有2个表格:细分和自定义属性。

Table defs are: 表定义是:

segments
- name - 名称
- organization_id -Organization_id
- ancestry -祖先
- ancestry_depth -ancestry_depth

custom_properties custom_properties
- name - 名称
- label - 标签
- value -价值
- segment_id -segment_id

Segments are tree-like structures, thus the ancestry columns. 段是树状结构,因此是祖先列。 eg Segment 3 is a child of Segment 2 is a child of Segment 1. Each of those segments will have the same custom properties, however, the values could come from an inherited segment (a segment up the tree) instead of from the segment itself. 例如,细分3是细分2的子级,细分2是细分1的子级。这些细分中的每个细分都具有相同的自定义属性,但是,值可能来自继承的细分(树上的某个细分),而不是来自细分本身。 If a value of a particular property is inherited from a segment up the tree, then its value is set to null. 如果某个特定属性的值是从树的某个部分继承的,则其值将设置为null。

My goal is to retrieve custom properties for a particular segment, in a single SQL statement, while taking into consideration the depth of the segment since depth determines the priority order of the values that I need. 我的目标是在单个SQL语句中检索特定段的自定义属性,同时考虑到段的深度,因为depth决定了我需要的值的优先级顺序。

The current query I have is very close, it returns the correct number of properties for a particular segment, however, with that group by statement I always get the property with the lowest ID, but what I really need is the property with the highest depth. 我当前的查询非常接近,它返回特定段的正确数量的属性,但是,通过该group by语句,我总是获得ID最低的属性,但我真正需要的是深度最大的属性。 If I take out the group by then I get duplicate properties, 1 from each segment listed in the "IN". 如果我取出组,则将得到重复的属性,“ IN”中列出的每个段均为1。 But again, I only need property from the highest depth. 但是同样,我只需要最深处的属性。

SELECT custom_properties.name, 
       custom_properties.value, 
       segments.ancestry_depth 
FROM   custom_properties 
       INNER JOIN segments 
         ON custom_properties.object_id = segments.id 
WHERE  custom_properties.object_type = 'Segment' 
       AND custom_properties.object_id IN ( 86770, 86637, 40667, 180 ) 
       AND custom_properties.value IS NOT NULL 
GROUP  BY custom_properties.name 
ORDER  BY custom_properties.name, 
          segments.ancestry_depth DESC 

I hope this makes sense. 我希望这是有道理的。 I appreciate any help and/or direction. 感谢您的帮助和/或指导。

Thank you! 谢谢!

Louis 路易

Try removing the DESC keyword from the ORDER BY clause. 尝试从ORDER BY子句中删除DESC关键字。

EDIT: 编辑:

Try the following: 请尝试以下操作:

SELECT v.*
FROM   (SELECT custom_properties.name, 
               custom_properties.value, 
               segments.ancestry_depth 
        FROM   custom_properties 
               INNER JOIN segments 
                 ON custom_properties.object_id = segments.id 
        WHERE  custom_properties.object_type = 'Segment' 
               AND custom_properties.object_id IN ( 86770, 86637, 40667, 180 ) 
               AND custom_properties.value IS NOT NULL 
        ORDER  BY custom_properties.name, 
                  segments.ancestry_depth DESC) v
GROUP  BY name 

Maby this wil work: Maby的工作原理是:

SELECT c.name, c.VALUE, s1.ancestry_depth, s2.ancestry_depth as depth
FROM custom_properties as c
JOIN segments s1 ON c.object_id = s1.id
LEFT JOIN segments s2 ON c.object_id = s2.id AND s2.ancestry_depth > s1.ancestry_depth
WHERE  c.object_type = 'Segment' 
AND c.object_id IN ( 86770, 86637, 40667, 180 ) 
AND c.VALUE IS NOT NULL 
GROUP  BY c.name 
HAVING ISNULL(depth)
ORDER  BY c.name, s1.ancestry_depth DESC 

edit: I misunderstood you and was selecting properties with biggest depth but i should select segnents with biggest depth 编辑:我误会了您,并且正在选择深度最大的属性,但是我应该选择深度最大的正弦

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

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