Sql 需要查看子查询的合并输出
我要看航班号,那次航班的票数,那次航班的工作人员(空姐)数 我可以通过2个子查询查看它们,但无法查看合并视图Sql 需要查看子查询的合并输出,sql,oracle,Sql,Oracle,我要看航班号,那次航班的票数,那次航班的工作人员(空姐)数 我可以通过2个子查询查看它们,但无法查看合并视图 select f.flightid, count(ticketnum) from flight f , ticket t where f.actDepartDateTime is not null and f.flightid=t.flightid group by f.flightid order b
select f.flightid, count(ticketnum) from flight f , ticket t
where f.actDepartDateTime is not null
and f.flightid=t.flightid
group by f.flightid
order by 1;
select f.flightid, count(h.staffid) from flight f ,hosting h
where f.actDepartDateTime is not null
and f.flightid=h.flightID
group by f.flightid
order by 1;
在所有三个表中使用这样一个
内部join
语句
select f.flightid,
count( case when nvl(t.ticketnum,0)>0 then 1 end ) as cnt_ticketnum,
count( case when nvl(h.staffid,0)>0 then 1 end ) as cnt_staffid
from flight f
join hosting h on f.flightid=h.flightID
join ticket t on f.flightid=t.flightid
where f.actDepartDateTime is not null
group by f.flightid
order by f.flightid;
简单解决方案-使用相关子查询进行计数。(无需担心多对多关系、多次加入、无票航班等)
你也可以使用联合嗨,你可以联合他们!为了理解联合运算符,你需要对h.staffid进行区分,不是吗?非常感谢,我试过了,但不知怎的,计数的正确答案是否定的coming@user2639287不客气。可能您的
t.ticketnum
或h.staffid
的值为空。因此,我更改了查询。
select f.flightid,
(select count(*) from ticket t where f.flightid = t.flightid) as cnt_ticketnum,
(select count(*) from hosting h where f.flightid = h.flightID) as cnt_staffid
from flight f
where f.actDepartDateTime is not null
order by f.flightid