我有两个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 条记录
,第二个查询显示 10 条记录
我如何组合这两个查询来获取单个查询结果以显示 11 条记录,其中 10 个值和 1 个空值
我已经尝试过此操作,但仅显示10条记录而不是空记录
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
请提供一些帮助。
最佳答案
分组正在消除您的第 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'
关于mysql - 我怎样才能组合两个mysql查询来得到一个结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22111612/