1

我正在尝试编写一个查询来告诉我每种颜色的数量Female

White - 2
Blue - 5
Green - 13

到目前为止,我有以下查询,其中一些尝试已被注释掉:

SELECT a.id AS aid, af.field_name AS aname, afv.field_value
FROM applications app, applicants a, application_fields af, application_fields_values afv, templates t, template_fields tf
WHERE a.application_id = app.id
AND af.application_id = app.id
AND afv.applicant_id = a.id
AND afv.application_field_id = af.id
#AND af.template_id = t.id
AND af.template_field_id = tf.id
AND t.id = tf.template_id
AND afv.created_at >= '2013-01-01' 
AND afv.created_at <= '2013-12-31' 
#AND af.field_name = 'Male' 
AND afv.field_value = 1
ORDER BY aid, aname
#GROUP BY aid, aNAME
#HAVING aname = 'Female';

目前,此查询返回如下数据:

aid |  aname   | field_value
    4  Female   1
    4  White    1
    5  Green    1
    5  Female   1
    6  Female   1
    6  White    1
    7  Blue     1
    7  Female   1
    8  Female   1
    8  Blue     1
    9  Male     1
    9  Green    1

表结构:

applications:
id

application_fields:
id
application_id
field_name

applications_fields_values:
id
application_field_id
applicant_id
field_value

template:
id

template_fields:
id
template_id

applicant:
id
application_id

样本数据:

application_fields
id | application_id | field_name |template_id | template_field_id
1  |        1       |     blue   |      1     |         1
2  |        1       |     green  |      1     |         2
3  |        1       |     female |      1     |         3

application_fields_values
id | application_field_id | applicant_id | field_value
4  |            1         |        1     |      1     
5  |            2         |        1     |      0     
6  |            3         |        1     |      1

templates
id |    name    |
1  | mytemplate |

template_fields
id | template_id | field_name |
1  |       1     |   blue
2  |       1     |   green
3  |       1     |   female

编辑

我很确定下面的查询得到了我正在寻找的东西,但是它非常慢,而且我最大的表的行数少于 30K。

询问

SELECT af.field_name AS aname, sum(afv.field_value) AS totals
    FROM applications app, applicants a, application_fields af, application_fields_values afv, templates t, template_fields tf
    WHERE a.application_id = app.id
    AND af.application_id = app.id
    AND afv.applicant_id = a.id
    AND afv.application_field_id = af.id
    AND af.template_field_id = tf.id
    AND t.id = tf.template_id
    AND afv.created_at >= '2013-01-01' 
    AND afv.created_at <= '2013-12-31' 
    AND afv.field_value = 1
    AND a.id IN (
        SELECT 
            a2.id
        FROM applications app2, applicants a2, application_fields af2, application_fields_values afv2, templates t2, template_fields tf2
        WHERE af2.application_id = app2.id
        AND afv2.applicant_id = a2.id
        AND afv2.application_field_id = af2.id
        AND af2.template_field_id = tf2.id
        AND t2.id = tf2.template_id
        AND afv2.created_at >= '2013-01-01' 
        AND afv2.created_at <= '2013-12-31' 
        #AND af2.field_name = 'Male' 
        AND af2.field_name = 'Female'
        AND afv2.field_value = 1
    )
    GROUP BY aname;

产生结果:

aname | totals
Green    2
Black    27
Blue     5
4

4 回答 4

4
SELECT f1.field_name, count(*) as total
  FROM application_fields f1
  JOIN applications_fields_values v1
    ON v1.application_field_id = f1.id
  JOIN applications_fields_values v2
    ON v1.applicant_id = v2.applicant_id
  JOIN applications_fields f2
    ON v2.application_field_id = f2.id
 WHERE v1.field_value = 1
   AND v2.field_value = 1
   AND f2.field_name = 'Female'
   AND f1.field_name != 'Female'
   AND f1.created_at BETWEEN '2013-01-01' AND '2013-12-31' 
 GROUP BY f1.field_name

除非您有其他要求,否则您似乎不需要参考表格templates, template_fields,applicationsapplicant来解决您的问题。此外,您如何识别application_fields代表颜色的方法也不是很清楚。如果您对此有更多信息,可能会添加一些条件。

于 2013-11-08T12:05:55.647 回答
0

查询看起来很好,只需在WHERE子句中添加条件,HAVING如果您希望过滤掉基于分组的结果,则应该在其中添加

试试这个

SELECT 
    af.field_name AS aname, 
    count(afv.field_value) as totals
FROM 
    applications app, 
    applicants a, 
    application_fields af, 
    application_fields_values afv, 
    templates t, 
    template_fields tf
WHERE 
    a.application_id = app.id
    AND af.application_id = app.id
    AND afv.applicant_id = a.id
    AND afv.application_field_id = af.id
    #AND af.template_id = t.id
    AND af.template_field_id = tf.id
    AND t.id = tf.template_id
    AND afv.created_at >= '2013-01-01' 
    AND afv.created_at <= '2013-12-31' 
    #AND af.field_name = 'Male' 
    AND afv.field_value = 1
    AND aname = 'Female'
ORDER BY 
    aname
GROUP BY 
    aNAME
于 2013-11-06T06:04:24.973 回答
0

尝试使用 EXISTS 函数代替 IN

SELECT af.field_name AS aname, sum(afv.field_value) AS totals
FROM applications app, applicants a, application_fields af, application_fields_values afv, templates t, template_fields tf
WHERE a.application_id = app.id
AND af.application_id = app.id
AND afv.applicant_id = a.id
AND afv.application_field_id = af.id
AND af.template_field_id = tf.id
AND t.id = tf.template_id
AND afv.created_at >= '2013-01-01' 
AND afv.created_at <= '2013-12-31' 
AND afv.field_value = 1
AND EXISTS (
    SELECT 
        1
    FROM applications app2, applicants a2, application_fields af2, application_fields_values afv2, templates t2, template_fields tf2
    WHERE af2.application_id = app2.id
    AND afv2.applicant_id = a2.id
    AND afv2.application_field_id = af2.id
    AND af2.template_field_id = tf2.id
    AND t2.id = tf2.template_id
    AND afv2.created_at >= '2013-01-01' 
    AND afv2.created_at <= '2013-12-31' 
    #AND af2.field_name = 'Male' 
    AND af2.field_name = 'Female'
    AND afv2.field_value = 1
    AND a.id = a2.id    -- add this condition
)
GROUP BY aname;

来源:http ://dev.mysql.com/doc/refman/5.5/en/optimizing-subqueries.html

注意:此修改可能会更快或更慢,具体取决于几个条件,如果它较慢请考虑该(源)页面底部的几个条件。

于 2013-11-08T06:24:43.913 回答
0

您应该考虑更清晰的设计。似乎有不同的问题。

例如,使用 template 和 template_fields 值(以及提供的示例数据),我可以猜测 [application fields] 源自 [templates],只要应用程序只有一个模板。在这种情况下,您可以为 application_fields_values 设计一个多对多表,如下所示:

applications:                   
id | template_id

application_fields_values:      
application_id | template_field_id | field_value

application fields: redundant

template information derived from template_fields

这适用于 [应用程序字段] 是否应该存在(即如果模板字段集被部分遵循),或者如果模板字段集是强制性的。

一般来说,您的表格似乎有多余的引用。

于 2013-11-13T16:24:08.507 回答