在相同的select语句中计算具有不同条件的相同id列

时间:2014-01-02 11:55:06

标签: mysql sql sql-server select count

我需要将两个select语句组合成一个选择

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
 LIMIT start,max;

第二个查询,唯一的区别是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
  LIMIT start,max;

4 个答案:

答案 0 :(得分:2)

以这种方式试试

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)

答案 1 :(得分:1)

SELECT count(tbl_employer_post_details.employer_id) PostDetails
FROM tbl_employer_post_details, tbl_employer_registration
WHERE job_status IN(0,1) AND  
tbl_employer_registration.employer_id=tbl_employer_post_details.employer_id LIMIT start,max;

SELECT SUM(job_status = 0) pending,
       SUM(job_status = 1) approved
FROM tbl_employer_post_details, tbl_employer_registration
WHERE job_status IN(0,1) AND  
tbl_employer_registration.employer_id=tbl_employer_post_details.employer_id LIMIT start,max;

答案 2 :(得分:0)

试试这个:

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);

答案 3 :(得分:0)

试试这样。

  SELECT SUM(case when job_status = 0 then 1 else 0 end) pending,
         SUM(case when job_status = 1 then 1 else 0 end) 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)