在单个查询

时间:2017-10-05 06:43:07

标签: mysql ruby-on-rails

以下是查询。请建议我如何简化相同的内容:

在查询中,我必须选择具有指定提交状态ID的电子邮件生成计数。如您所见,我必须从同一个表中获取计数,但我必须为不同的提交状态集提供单独的查询。

SELECT  a.id, a.created_at, a.recruiter_id,
           CONCAT_WS(' ', first_name, middle_name, last_name) as f_name,

(select count(*) from email_generations where email_generations.candidate_id = a.id and
 email_generations.submission_status_id = 2) as subm_count,

SUM((select count(*) from submission_statuses where submission_statuses.id IN (7,8) and submission_statuses.id =
 email_generations.submission_status_id)) as phn_intw_count,

SUM((select count(*) from submission_statuses where submission_statuses.id IN (9,10) and submission_statuses.id =
 email_generations.submission_status_id)) as intw_count,

SUM((select count(*) from submission_statuses where submission_statuses.id IN (12,13) and submission_statuses.id =
 email_generations.submission_status_id)) as offer_count,

SUM((select count(*) from submission_statuses where submission_statuses.id IN (11) and submission_statuses.id =
 email_generations.submission_status_id)) as ref_chk_count

FROM candidates AS a

INNER JOIN email_generations ON email_generations.candidate_id = a.id WHERE a.deleted_at IS NULL

GROUP BY a.id HAVING a.recruiter_id = 1 and (subm_count <> 0 OR phn_intw_count <> 0 OR intw_count <> 0 OR offer_count <> 0 OR
 ref_chk_count <> 0)

表:Email Generation Table Structure, Submission Statuses Table Structure

**所需的输出应该类似于以下示例 - **

&#13;
&#13;
<table border="0" cellspacing="0">
	<colgroup span="2" width="85">
	</colgroup>
	<colgroup width="174">
	</colgroup>
	<colgroup width="85">
	</colgroup>
	<colgroup width="110">
	</colgroup>
	<colgroup span="2" width="85">
	</colgroup>
	<colgroup width="92">
	</colgroup>
	<tbody>
		<tr>
			<td align="left" height="17"><b>Candidate ID</b></td>
			<td align="left"><b>Recruiter ID</b></td>
			<td align="left"><b>Candidate Name</b></td>
			<td align="left"><b>subm_count</b></td>
			<td align="left"><b>phn_intw_count</b></td>
			<td align="left"><b>intw_count</b></td>
			<td align="left"><b>offer_count</b></td>
			<td align="left"><b>ref_chk_count</b></td>
		</tr>
		<tr>
			<td align="right" height="17" sdnum="16393;" sdval="258">11258</td>
			<td align="right" sdnum="16393;" sdval="1">1</td>
			<td align="left">Test Candidate</td>
			<td align="right" sdnum="16393;" sdval="0">4</td>
			<td align="right" sdnum="16393;" sdval="0">2</td>
			<td align="right" sdnum="16393;" sdval="0">9</td>
			<td align="right" sdnum="16393;" sdval="1">1</td>
			<td align="right" sdnum="16393;" sdval="0">3</td>
		</tr>
	</tbody>
</table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

感谢朋友们的优质时光。我自己对查询进行了优化,并希望与大家分享。

感谢@ user3366016提示从电子邮件生成而不是候选人那里获取记录。

  

SELECT DISTINCT candidates.id,candidates.created_at,   candidates.recruiter_id,CONCAT_WS(&#39;&#39;,candidates.first_name,   candidate.middle_name,candidates.last_name)为f_name,

     

(select count(*)FROM email_generations AS e WHERE e.submission_status_id = 2 and e.candidate_id = candidates.id)AS   subm_count,

     

(选择count(*)FROM email_generations AS e WHERE e.submission_status_id IN(7,8)和e.candidate_id = candidates.id)AS   phn_intw_count,

     

(选择计数(*)FROM email_generations AS e WHERE e.submission_status_id IN(9,10)和e.candidate_id = candidates.id)   AS intw_count,

     

(选择计数(*)FROM email_generations AS e WHERE e.submission_status_id IN(12,13)和e.candidate_id = candidates.id)   AS offer_count,

     

(选择count(*)FROM email_generations AS e WHERE e.submission_status_id IN(11)和e.candidate_id = candidates.id)AS   ref_chk_count

     

FROM email_generations

     

INNER JOIN候选人候选人.id = email_generations.candidate_id AND candidates.deleted_at IS NULL

     

WHERE email_generations.deleted_at为空且(candidate.recruiter_id = 1和   email_generations.submission_status_id IN(2,7,8,9,10,12,13,11))

     

HAVING(subm_count&lt;&gt; 0 OR phn_intw_count&lt;&gt; 0 OR intw_count&lt;&gt; 0 OR offer_count&lt;&gt; 0 OR ref_chk_count&lt;&gt; 0)