mysql - 尝试编写复杂的查询来检索数据

标签 mysql sql

我正在尝试编写一个查询来告诉我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

最佳答案

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

您似乎不需要引用表 templatestemplate_fieldsapplicationsapplicant解决你的问题,除非你有额外的要求。此外,您还不清楚如何识别哪些 application_fields 代表颜色。如果您有更多相关信息,可能会添加一些条件。

关于mysql - 尝试编写复杂的查询来检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19804457/

相关文章:

java - 更快地批量加载嵌套数据

mysql - 列名前的 SQL 语句中的 At-Sign

php - Mysqli 准备语句 num_rows 带有多个参数

c# - MySql "Select Where"和 C#

Android 数据库 - 没有这样的表错误

sql - 将 SQL 结果传递到 shell 脚本中的数组时标识符无效

php - Mysql-查询: How to find task assiged to which developer

raspberry-pi - 如何将 MySQL 数据从 Raspberry Pi 传输到我的电脑

SQL:如何正确检查记录是否存在

SQL - 使用条件获取多个计数