简体   繁体   English

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

[英]Count same id column with different condition in same select statement

I need to combine both select statements into one 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;

And the second query, with the only difference being the WHERE job_status=1 : 第二个查询,唯一的区别是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;

Try it this way 这样尝试

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

Try this: 尝试这个:

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

Try it like this. 这样尝试。

  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)

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

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