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
Mysql 如何根据另一个表的主键获取一个表的两列计数?_Mysql_Sql - Fatal编程技术网

Mysql 如何根据另一个表的主键获取一个表的两列计数?

Mysql 如何根据另一个表的主键获取一个表的两列计数?,mysql,sql,Mysql,Sql,我有两个表定义为: CREATE TABLE airports ( code char(3) not null primary key, name varchar(100) not null, city varchar(50) not null, state varchar(5), country char(2) not null references fcountries(code) ) 及 我需要使用postgres获取从机场起飞和到达机场的航班数量。所以,我写了 se

我有两个表定义为:

CREATE TABLE airports (
  code char(3) not null primary key,
  name varchar(100) not null,
  city varchar(50) not null,
  state varchar(5),
  country char(2) not null references fcountries(code)
)

我需要使用postgres获取从机场起飞和到达机场的航班数量。所以,我写了

select
  code,
  count(departure)
from airports 
join flights 
 on flights.departure = airports.code group by code;
同样地,对于离开

select
  code,
  count(arrival) as Arrival
from airports 
join flights 
  on flights.arrival = airports.code group by code;
但是我需要把这两个结合起来,得到相同结果中的到达-离开计数。我不知道怎么做? 我写了这个查询

select
  code,
  count(departure),
  x.arrival
from (
  select count(arrival) as arrival
  from airports 
  join flights 
    on flights.arrival = airports.code group by code
) x, 
airports 
join flights on airports.code = flights.departure
group by code, x.arrival
order by code;

但结果不正确,因为此结果中重复了“代码”。我对sql非常陌生,不知道如何编写它。

您可以将这两个查询用作子查询和联接

    select t1.code,  t1.count_departure, t2.count_arrival 
    from (
        select code, count(departure)  count_departure
        from airports join flights on flights.departure = airports.code 
        group by code

    ) t1  
    inner join  (
        select code, count(arrival) as  count_arrival 
        from airports join flights on flights.arrival = airports.code 
        roup by code

    ) t2 on t1.code  = t2.code ;

我认为alias t2在加入后不能像那样使用,这就是为什么会出现错误。试试这个:

选择t1.code、t1.count\U出发、t2.count\U到达 从…起 选择代码、计数出发点计数\出发点 从机场加入航班。出发=机场。代码 按代码分组 t1 参加 选择代码,将countarrival作为count\u arrival 从机场加入航班。到达=机场。代码 按代码分组
t1上的t2。代码=t2。代码

最简单的方法是横向连接和分组:


请注意,加入机场是不必要的,除非您确实想引入问题中未提及的更多信息。

一个非常简单的方法就是在SELECT子句中使用相关计数子查询:

select a.code,
    (select count(*) from flights f where f.departure = a.code) as departures,
    (select count(*) from flights f where f.arrival = a.code) as arrivals
from airports a;

更新您的问题,添加相关的表架构一个适当的数据样本和预期的结果,并决定DBMS请。我猜是打字错误。按代码分组而不是按代码分组我更改了,但错误仍然存在。这在选择附近给出了错误语法错误,即t2select代码之后的select。。。。我不知道为什么?答案更新了。。可插入别名的位置错误
select v.code, count(*) as total,
       sum(is_departure) as num_departures,
       sum(is_arrival) as num_arrivals
from flights cross join lateral
     (values (departure, 1, 0), (arrival, 0, 1)
     ) as v(code, is_departure, is_rrival)
     on f.departure = a.code
group by v.code;
select a.code,
    (select count(*) from flights f where f.departure = a.code) as departures,
    (select count(*) from flights f where f.arrival = a.code) as arrivals
from airports a;