GROUP BY 导致不良结果
GROUP BY resulting in undesirable results
在下面的查询中,我提取了一份特定工作的求职者名单。无论我按什么分组,它要么给我相同的用户但每个应用程序数据不同,要么我为每个返回 4 行。
我刚刚开始接触关系数据库设计,所以我假设我的设置有误。我将申请人和申请分开,现在我正在努力对数据进行分组。除非我只需要一个子查询,group_concat,或者只是错误地使用了 GROUP BY?
SELECT
applicants.*, applications.*, users.*
FROM applicants
INNER JOIN applications ON applicants.job_id = applications.job_id
INNER JOIN users ON applicants.user_id = users.user_id
WHERE
applicants.job_id = 56 AND applicants.process_level = 1
GROUP BY applications.app_id
Table: 申请人
+-----+--------+---------+--------+--------------------+---------------+
| id | job_id | user_id | app_id | applied_on | process_level |
+-----+--------+---------+--------+--------------------+---------------+
| 1 | 56 | 125 | 5 |2015-05-24 19:28:55 | 1 |
| 2 | 22 | 15 | 6 |2015-05-25 16:38:24 | 2 |
| 3 | 56 | 100 | 7 |2015-05-26 13:38:24 | 1 |
+-----+--------+---------+--------+--------------------+---------------+
Table:应用程序
+---------+--------+---------+--------------+-------------+
| app_id | job_id | user_id | experience | location |
+---------+--------+---------+--------------+-------------+
| 5 | 56 | 125 | bibendum jus | suscipi |
| 6 | 22 | 15 | Morbi vitae | aliquet |
| 7 | 56 | 100 | Duis et ex a | convallis |
+---------+--------+---------+--------------+-------------+
Table:用户
+---------+-----------------+------------+-----------+
| user_id | user_email | first_name | last_name |
+---------+-----------------+------------+-----------+
| 15 | joes@email.com | Joe | Shcomo |
| 100 | sally@email.com | Sally | Smith |
| 125 | johnj@email.com | John | Doe |
+---------+-----------------+------------+-----------+
想要的结果:例如我想要用户 125 的一行,其中 job_id 是 56,其中包含来自申请人、应用程序和用户的所有数据。 (当然不是所有数据都代表):
+---------+------------+-----------+---------------+---------------------+
| user_id | first_name | location | process_level | applied_on |
+---------+------------+-----------+---------------+---------------------+
| 125 | John | suscipi | 1 | 2015-05-24 19:28:55 |
| 100 | Sally | convallis | 1 | 2015-05-26 13:38:24 |
+---------+------------+-----------+---------------+---------------------+
首先,关于规范化的注意事项:你不应该在申请人和应用程序 table 中存储 job_id 和 user_id。可能,您只需要 'application' table 中的它们,因为我可以从申请人 => 申请中确定该信息。通过将这些关系存储在两个 table 中,您可以为自己不喜欢的异常敞开心扉。
话虽如此,您不需要按任何内容进行分组。您可以使用已有的 JOIN
获得用户和应用程序之间的一致关系。我通过使用在 applications
和 applicants
之间相关的 user_id、job_id 和 app_id 列加强了这些连接:
SELECT u.user_id, u.first_name, a.location, ap.process_level, ap.applied_on
FROM users u
JOIN applications a ON a.user_id = u.user_id
JOIN applicants ap ON ap.user_id = a.user_id AND ap.app_id = a.app_id AND ap.job_id = a.job_id
WHERE ap.job_id = 56 AND ap.process_level = 1;
这在 SQL Fiddle 中非常有效。
在下面的查询中,我提取了一份特定工作的求职者名单。无论我按什么分组,它要么给我相同的用户但每个应用程序数据不同,要么我为每个返回 4 行。
我刚刚开始接触关系数据库设计,所以我假设我的设置有误。我将申请人和申请分开,现在我正在努力对数据进行分组。除非我只需要一个子查询,group_concat,或者只是错误地使用了 GROUP BY?
SELECT
applicants.*, applications.*, users.*
FROM applicants
INNER JOIN applications ON applicants.job_id = applications.job_id
INNER JOIN users ON applicants.user_id = users.user_id
WHERE
applicants.job_id = 56 AND applicants.process_level = 1
GROUP BY applications.app_id
Table: 申请人
+-----+--------+---------+--------+--------------------+---------------+
| id | job_id | user_id | app_id | applied_on | process_level |
+-----+--------+---------+--------+--------------------+---------------+
| 1 | 56 | 125 | 5 |2015-05-24 19:28:55 | 1 |
| 2 | 22 | 15 | 6 |2015-05-25 16:38:24 | 2 |
| 3 | 56 | 100 | 7 |2015-05-26 13:38:24 | 1 |
+-----+--------+---------+--------+--------------------+---------------+
Table:应用程序
+---------+--------+---------+--------------+-------------+
| app_id | job_id | user_id | experience | location |
+---------+--------+---------+--------------+-------------+
| 5 | 56 | 125 | bibendum jus | suscipi |
| 6 | 22 | 15 | Morbi vitae | aliquet |
| 7 | 56 | 100 | Duis et ex a | convallis |
+---------+--------+---------+--------------+-------------+
Table:用户
+---------+-----------------+------------+-----------+
| user_id | user_email | first_name | last_name |
+---------+-----------------+------------+-----------+
| 15 | joes@email.com | Joe | Shcomo |
| 100 | sally@email.com | Sally | Smith |
| 125 | johnj@email.com | John | Doe |
+---------+-----------------+------------+-----------+
想要的结果:例如我想要用户 125 的一行,其中 job_id 是 56,其中包含来自申请人、应用程序和用户的所有数据。 (当然不是所有数据都代表):
+---------+------------+-----------+---------------+---------------------+
| user_id | first_name | location | process_level | applied_on |
+---------+------------+-----------+---------------+---------------------+
| 125 | John | suscipi | 1 | 2015-05-24 19:28:55 |
| 100 | Sally | convallis | 1 | 2015-05-26 13:38:24 |
+---------+------------+-----------+---------------+---------------------+
首先,关于规范化的注意事项:你不应该在申请人和应用程序 table 中存储 job_id 和 user_id。可能,您只需要 'application' table 中的它们,因为我可以从申请人 => 申请中确定该信息。通过将这些关系存储在两个 table 中,您可以为自己不喜欢的异常敞开心扉。
话虽如此,您不需要按任何内容进行分组。您可以使用已有的 JOIN
获得用户和应用程序之间的一致关系。我通过使用在 applications
和 applicants
之间相关的 user_id、job_id 和 app_id 列加强了这些连接:
SELECT u.user_id, u.first_name, a.location, ap.process_level, ap.applied_on
FROM users u
JOIN applications a ON a.user_id = u.user_id
JOIN applicants ap ON ap.user_id = a.user_id AND ap.app_id = a.app_id AND ap.job_id = a.job_id
WHERE ap.job_id = 56 AND ap.process_level = 1;
这在 SQL Fiddle 中非常有效。