Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 需要查看子查询的合并输出_Sql_Oracle - Fatal编程技术网

Sql 需要查看子查询的合并输出

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

我要看航班号,那次航班的票数,那次航班的工作人员(空姐)数

我可以通过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 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