Friday, 3 January 2014

Combine both select statements into one select with different condition

Combine both select statements into one select with different condition
Lekhulal
lekulal@gmail.com 
----------------------------------------

Example:

The first query, with the only difference being the 
WHERE job_status=0



SELECT count(tbl_employer_post_details.employer_id) pending
FROM tbl_employer_post_details,tbl_employer_registration
WHERE job_status=0 
AND tbl_employer_registration.employer_id = 
    tbl_employer_post_details.employer_id;
 
The second query, with the only difference being the  
WHERE job_status=1
 
SELECT count(tbl_employer_post_details.employer_id) approved
FROM tbl_employer_post_details,tbl_employer_registration
WHERE job_status=1 
AND tbl_employer_registration.employer_id = 
    tbl_employer_post_details.employer_id;
 
Solution:
 
SELECT SUM(job_status = 0) pending, 
SUM(job_status = 1) approved 
FROM tbl_employer_post_details epd 
INNER JOIN tbl_employer_registration er  
ON epd.employer_id = er.employer_id
WHERE job_status IN (0, 1);
 
(OR)
 
SELECT SUM(job_status = 0) pending,
SUM(job_status = 1) approved
FROM tbl_employer_post_details d  
JOIN tbl_employer_registration r
ON r.employer_id = d.employer_id
WHERE job_status IN (0, 1)   

No comments:

Post a Comment