在下面的查询中,我拉出了特定作业上的申请人列表。无论我GROUP BY如何,它或者给我的用户都是同一个用户,但是每个应用程序的数据都不相同,或者我为每个用户返回4行。GROUP BY导致不良结果
我刚开始进入关系数据库设计,所以我假设我把它设置错了。我将申请人和申请分开,现在我正在努力分组数据。除非我只需要一个子查询,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
表:申请人
+-----+--------+---------+--------+--------------------+---------------+
| 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 |
+-----+--------+---------+--------+--------------------+---------------+
表:应用
+---------+--------+---------+--------------+-------------+
| 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 |
+---------+--------+---------+--------------+-------------+
表:用户
+---------+-----------------+------------+-----------+
| user_id | user_email | first_name | last_name |
+---------+-----------------+------------+-----------+
| 15 | [email protected] | Joe | Shcomo |
| 100 | [email protected] | Sally | Smith |
| 125 | [email protected] | John | Doe |
+---------+-----------------+------------+-----------+
所需的结果:例如我想要job_id为56的用户125的单行,其中包含来自申请人,应用程序和用户的所有数据。 (未表示当然所有数据):
+---------+------------+-----------+---------------+---------------------+
| 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 |
+---------+------------+-----------+---------------+---------------------+
也许你根本不需要'group by'。 –
'applicants。*,applications。*,users。*'不适合'GROUP BY',既然你没有要求任何聚合值,你不需要分组 –
我可能错过了它,但是我不确定你是否真的说出你想要做的事情。 –