2014-03-01 90 views
0

我有兩個MySQL查詢如下我如何組合兩個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 

請提供一些幫助。

+0

您可以使用union。請看這裏stackoverflow.com/questions/5331808/how-do-i-combine-the-results-of-two-queries-with-ordering – bring2dip

+0

@avisheks我已經添加了兩個表查詢輸出和結果輸​​出,我想請幫助 – user3364940

+0

使它正確的外部加入 – avisheks

回答

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 
+0

我得到了同樣的結果,因爲我從我的查詢中獲得。 – user3364940

+0

你試過外接? –

+0

我已更新問題和我想要的結果 – user3364940

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' 
相關問題