我正在运行一个查询,我正在做一些结果的联合。现在查询的工作原理,默认情况下有6行。现在联合会给那些行提供6个结果。如果所有联合都有数据那么没问题,我得到6行,但问题是如果任何一个联盟没有数据显示然后我没有得到6行,而是我得到4行或有时2。但我想要哪个查询不会有数据它将返回0那里。我发布我的查询
select count(distinct CANDYS.candidateId) from
(Select C.candidateID,J.joborderID from db_candidatenote C, db_jobordernote J
where C.noteid=J.noteid and C.action like 'Engaged'
and C.dateadded between '2014-04-01' and '2014-04-30' ) N,
(Select distinct Note.candidateId, JNote.jobOrderID
from db_candidatenote Note,db_jobordernote JNote, corporateuser User where
Note.noteid=JNote.noteid
and Note.commentingPersonID = User.userid
and User.firstName='Avishek'
and Note.action='Sourcing') CANDYS
where N.candidateID= CANDYS.candidateId
and N.joborderid=CANDYS.jobOrderID
union
select count(distinct CANDYS.candidateId) from
(Select C.candidateID,J.joborderID from db_candidatenote C, db_jobordernote J
where C.noteid=J.noteid and C.action like 'Qualification'
and C.dateadded between '2014-04-01' and '2014-04-30' ) N,
(Select distinct Note.candidateId, JNote.jobOrderID
from db_candidatenote Note,db_jobordernote JNote, corporateuser User where
Note.noteid=JNote.noteid
and Note.commentingPersonID = User.userid
and User.firstName='Avishek'
and Note.action='Sourcing') CANDYS
where N.candidateID= CANDYS.candidateId
and N.joborderid=CANDYS.jobOrderID
union
select count(distinct CANDYS.candidateId) from
(Select C.candidateID,J.joborderID from db_candidatenote C, db_jobordernote J
where C.noteid=J.noteid and C.action like 'Internal Submission'
and C.dateadded between '2014-04-01' and '2014-04-30' ) N,
(Select distinct Note.candidateId, JNote.jobOrderID
from db_candidatenote Note,db_jobordernote JNote, corporateuser User where
Note.noteid=JNote.noteid
and Note.commentingPersonID = User.userid
and User.firstName='Avishek'
and Note.action='Sourcing') CANDYS
where N.candidateID= CANDYS.candidateId
and N.joborderid=CANDYS.jobOrderID
union
select count(distinct CANDYS.candidateId) from sendout S,
(Select distinct Note.candidateId, JNote.jobOrderID
from db_candidatenote Note,db_jobordernote JNote, corporateuser User where
Note.noteid=JNote.noteid
and Note.commentingPersonID = User.userid
and User.firstName='Avishek'
and Note.action='Sourcing' ) CANDYS
where S.candidateID= CANDYS.candidateId
and S.joborderid=CANDYS.jobOrderID
and S.dateadded between '2014-04-01' and '2014-04-30'
union
select count(distinct CANDYS.candidateId) from
(Select C.candidateID,J.joborderID from db_candidatenote C, db_jobordernote J
where C.noteid=J.noteid and C.action like '%Interview%'
and C.dateadded between '2014-04-01' and '2014-05-30' ) N,
(Select distinct Note.candidateId, JNote.jobOrderID
from db_candidatenote Note,db_jobordernote JNote, corporateuser User where
Note.noteid=JNote.noteid
and Note.commentingPersonID = User.userid
and User.firstName='Avishek'
and Note.action='Sourcing') CANDYS
where N.candidateID= CANDYS.candidateId
and N.joborderid=CANDYS.jobOrderID
union
select count(distinct CANDYS.candidateId) from
(Select C.candidateID,J.joborderID from db_candidatenote C, db_jobordernote J
where C.noteid=J.noteid and C.action like 'Support'
and C.dateadded between '2014-04-01' and '2014-04-30' ) N,
(Select distinct Note.candidateId, JNote.jobOrderID
from db_candidatenote Note,db_jobordernote JNote, corporateuser User where
Note.noteid=JNote.noteid
and Note.commentingPersonID = User.userid
and User.firstName='Avishek'
and Note.action='Sourcing') CANDYS
where N.candidateID= CANDYS.candidateId
and N.joborderid=CANDYS.jobOrderID
union
select count(distinct CANDYS.candidateId )from placement P,(Select distinct Note.candidateId, JNote.jobOrderID
from db_candidatenote Note,db_jobordernote JNote, corporateuser User where
Note.noteid=JNote.noteid
and Note.commentingPersonID = User.userid
and User.firstName='Avishek'
and Note.action='Sourcing') CANDYS
where P.candidateID= CANDYS.candidateId
and P.joborderid=CANDYS.jobOrderID
and P.dateadded between '2014-04-01' and '2014-04-30'
这是我的查询。因此,此查询将给出每个部分的结果。现在,如果任何查询没有数据,则它不应该为空,而是在结果处显示0。如果这样做。有人请帮帮忙。
就像它显示2行的图像一样。也不确定它显示的是哪个行数据。
答案 0 :(得分:0)
为简化问题,让我们假设这个架构:
create table t ( i int, a char(1) );
insert into t values ( 1, 'a');
在这种情况下,当where
子句中没有包含该行时,我们需要一个带有zero
的虚拟行作为数据。我们可以通过vars实现这一目标:
SET @f=0;
select i,a, @f:=1
from t
where i<>1 --< row not included
union
select 0,'0', @f as f --< fictitious row,only if f=0
from ( select 1 ) tt
having f=0 --< f is 0
结果:
i a
0 '0'
当包含一行时:
SET @f=0;
select i,a, @f:=1
from t
where i=1 --< row is included
union
select 0,'0', @f as f
from ( select 1 ) tt
having f=0 --< f is 1
结果:
i a
1 'a'
通过此剪辑,您可以推断代码中所需的更改。用于简化问题以简化帮助。 了解How to create a Minimal, Complete, and Verifiable example