简体   繁体   English

MySQL计算外键的不同ID

[英]Mysql count the different id's of the foreign key

I have the following tables: 我有以下表格:

jobs: 工作:

-------------------------------------------------------
| id | title | slug | 
-------------------------------------------------------

employments: 就业:

-------------------------------------------------------
| id | job_type| 
-------------------------------------------------------

applications: 应用:

-------------------------------------------------------
| id | job_opening_id| application_state_id| 
-------------------------------------------------------

application_states application_states

-------------------------------------------------------
| id | name| 
-------------------------------------------------------

I want to create a query that counts the different application_state_id's 我想创建一个查询,计算不同的application_state_id

----------------------------------------------------------------------------
| j.title| j.slug| e.job_type | candidates | hired
----------------------------------------------------------------------------

This is the query that i have at the moment: 这是我目前的查询:

SELECT
  j.title,
  j.slug,
  e.job_type,
  count(a1.application_state_id) as candidates,
  count(a2.application_state_id) as hired
FROM
  jobs AS j
INNER JOIN employments AS e ON j.employment_id = e.id
LEFT JOIN applications AS a1 ON a1.job_opening_id = job_openings.id
LEFT JOIN application_states AS as ON as.id = a1.application_state_id
LEFT JOIN applications AS a2 ON a2.job_opening_id = j.id AND a2.application_state_id = 1
GROUP BY 
  a1.application_state_id,
  a2.application_state_id,
  j.id,
  j.title,
  j.slug

I thought i could create 2 joins and set the application_state_id, but all that does is count records double. 我以为我可以创建2个联接并设置application_state_id,但是所有要做的就是计数记录增加一倍。 What do i need to change in this query? 我需要在此查询中进行哪些更改? I hope someone can help me. 我希望有一个人可以帮助我。

You did not provide sample data, but as I see from your code 您没有提供示例数据,但是正如我从您的代码中看到的那样
you are joining the table applications twice, 您要两次加入表格applications
so by the 1st to get the total number of candidates 因此,通过1号获得候选人总数
and by the 2nd to get the total number of hired candidates. 并通过第二次获得聘用的候选人总数。
I think you can drop the 2nd join and do conditional counting to get the total number of hired candidates. 我认为您可以放弃第二次加入并进行有条件的计数,以获取聘用候选人的总数。
Also: 也:
the select statement must include the columns that you group by and any aggregated columns select语句必须包括您分组的列和任何聚合的列
and I don't see why you need to join to the application_states table. 而且我看不到为什么需要加入到application_states表中。
Try this: 尝试这个:

SELECT
  j.title,
  j.slug,
  e.job_type,
  count(a.application_state_id) as candidates,
  sum(case when a.application_state_id = 1 then 1 else 0 end) as hired
FROM 
  jobs AS j INNER JOIN employments AS e ON j.employment_id = e.id
  LEFT JOIN applications AS a ON a.job_opening_id = job_openings.id
GROUP BY 
  j.title,
  j.slug,
  e.job_type

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

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