我如何结合两个mysql查询来获得一个结果?

时间:2014-03-01 07:38:53

标签: mysql sql

我有两个mysql查询,如下所示

SELECT cv_requirement,cv_target,cv_target_date_for 
FROM `cv_target` 
where cv_target_date_for between '2014-02-20' and '2014-02-27' and cv_recruiter='44'

AND

select count(candidate_id)as achi,cv_target_date from candidate
where fk_posted_user_id='44'
and cv_target_date between '2014-02-20' and '2014-02-27'
group by fk_job_id,cv_target_date

第一个查询生成11 record,第二个查询显示10 record 我如何结合这两个查询来获得单个查询结果来显示11 record with 10 values and 1 null value

我试过这个,但只显示10 record not null record

SELECT cv_requirement,cv_target,cv_target_date_for,count(candidate_id) achi, cv_target_date
FROM `cv_target` a
left join candidate b 
on a.cv_requirement=b.fk_job_id and a.cv_target_date_for=b.cv_target_date
where cv_target_date_for between '2014-02-20' and '2014-02-27' and cv_recruiter='44'
group by cv_requirement,cv_target

query one
    fk_job_id   achi    cv_target_date Ascending
    188         1       2014-02-20
    220         1       2014-02-20
    220         1       2014-02-21
    221         5       2014-02-21
    224         1       2014-02-22
    224         2       2014-02-24
    224         2       2014-02-25
    222         1       2014-02-25
    222         3       2014-02-26
    224         1       2014-02-27


query two

cv_requirement  cv_target   cv_target_date_for
188             2           2014-02-20
220             2           2014-02-21
221             2           2014-02-21
224             3           2014-02-22
220             1           2014-02-22
224             2           2014-02-24
222             1           2014-02-24
224             4           2014-02-25
222             4           2014-02-25
222             3           2014-02-26
224             3           2014-02-27


i want this out put

cv_requirement  cv_target   cv_target_date_for  achi
188             2           2014-02-20          1
220             2           2014-02-21          1
221             2           2014-02-21          5
224             3           2014-02-22          1
220             1           2014-02-22          0
224             2           2014-02-24          2
222             1           2014-02-24          0
224             4           2014-02-25          2
222             4           2014-02-25          1
222             3           2014-02-26          3
224             3           2014-02-27          1

请提供一些帮助。

2 个答案:

答案 0 :(得分:1)

分组正在消除您的第11条记录。您应该在内部查询中执行group by,然后才加入这两个:

SELECT          cv_requirement, cv_target, cv_target_date_for, achi
FROM            cv_target a
LEFT OUTER JOIN (SELECT   COUNT(candidate_id) AS achi, cv_target_date 
                 FROM     candidate
                 GROUP BY fk_job_id,cv_target_date) 
ON              a.cv_requirement = b.fk_job_id AND 
                a.cv_target_date_for = b.cv_target_date
WHERE           cv_target_date_for BETWEEN '2014-02-20' AND '2014-02-27' AND 
                cv_recruiter = '44'

答案 1 :(得分:0)

试试这个

 SELECT cv_requirement,cv_target,cv_target_date_for, achi
  FROM (SELECT cv_requirement,cv_target,cv_target_date_for 
   FROM `cv_target` 
   where cv_target_date_for between '2014-02-20' and '2014-02-27' and cv_recruiter='44') a

   left outer  join
    (
    select count(candidate_id)as achi,cv_target_date from candidate
  where fk_posted_user_id='44'
  and cv_target_date between '2014-02-20' and '2014-02-27'
  group by fk_job_id,cv_target_date) b

  on a.cv_requirement=b.fk_job_id and a.cv_target_date_for=b.cv_target_date