Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.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/4/postgresql/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
postgreSQL-不正确的查询结果_Sql_Postgresql_Join_Subquery - Fatal编程技术网

postgreSQL-不正确的查询结果

postgreSQL-不正确的查询结果,sql,postgresql,join,subquery,Sql,Postgresql,Join,Subquery,我有以下两个问题: Return the number of flights for each manufacturer: SELECT COUNT(flight) AS num_flights, manufacturer FROM flights, planes WHERE flights.tailnum = planes.tailnum GROUP BY manufacturer ORDER BY num_flights DESC 返回此处不可见的所有结果,总共27行: --------

我有以下两个问题:

Return the number of flights for each manufacturer:

SELECT COUNT(flight) AS num_flights, manufacturer
FROM flights, planes
WHERE flights.tailnum = planes.tailnum
GROUP BY manufacturer
ORDER BY num_flights DESC
返回此处不可见的所有结果,总共27行:

-------------+---------------------------------+--+--+--+
| num_flights | manufacturer                    |  |  |  |
+-------------+---------------------------------+--+--+--+
| 67623       | "BOEING"                        |  |  |  |
+-------------+---------------------------------+--+--+--+
| 36244       | "AIRBUS INDUSTRIE"              |  |  |  |
+-------------+---------------------------------+--+--+--+
| 11676       | "AIRBUS"                        |  |  |  |
+-------------+---------------------------------+--+--+--+
| 8932        | "MCDONNELL DOUGLAS AIRCRAFT CO" |  |  |  |
+-------------+---------------------------------+--+--+--+
| 4856        | "EMBRAER"                       |  |  |  |
+-------------+---------------------------------+--+--+--+
| 3998        | "MCDONNELL DOUGLAS"             |  |  |  |
+-------------+---------------------------------+--+--+--+
| 1259        | "MCDONNELL DOUGLAS CORPORATION" |  |  |  |
+-------------+---------------------------------+--+--+--+
| 247         | "CESSNA"                        |  |  |  |
+-------------+---------------------------------+--+--+--+
| 162         | "PIPER"                         |  |  |  |
+-------------+---------------------------------+--+--+--+
| 65          | "BELL"                          |  |  |  |
+-------------+---------------------------------+--+--+--+
| 63          | "DEHAVILLAND"                   |  |  |  |
+-------------+---------------------------------+--+--+--+
| 63          | "FRIEDEMANN JON"                |  |  |  |
+-------------+---------------------------------+--+--+--+
| 55          | "STEWART MACO"                  |  |  |  |
+-------------+---------------------------------+--+--+--+
| 54          | "LAMBERT RICHARD"               |  |  |  |
+-------------+---------------------------------+--+--+--+
| 51          | "KILDALL GARY"                  |  |  |  |
+-------------+---------------------------------+--+--+--+
| 47          | "BEECH"                         |  |  |  |
+-------------+---------------------------------+--+--+--+
| 44          | "MARZ BARRY"                    |  |  |  |
+-------------+---------------------------------+--+--+--+
| 42          | "AMERICAN AIRCRAFT INC"         |  |  |  |
+-------------+---------------------------------+--+--+--+
| 40          | "LEBLANC GLENN T"               |  |  |  |
+-------------+---------------------------------+--+--+--+
| 32          | "AGUSTA SPA"                    |  |  |  |
+-------------+---------------------------------+--+--+--+
| 27          | "SIKORSKY"                      |  |  |  |
+-------------+---------------------------------+--+--+--+
| 25          | "PAIR MIKE E"                   |  |  |  |
+-------------+---------------------------------+--+--+--+
| 22          | "DOUGLAS"                       |  |  |  |
+-------------+---------------------------------+--+--+--+
| 19          | "LEARJET INC"                   |  |  |  |
+-------------+---------------------------------+--+--+--+
| 18          | "AVIAT AIRCRAFT INC"            |  |  |  |
+-------------+---------------------------------+--+--+--+
| 17          | "HURLEY JAMES LARRY"            |  |  |  |
+-------------+---------------------------------+--+--+--+
| 13          | "GULFSTREAM AEROSPACE"          |  |  |  |
+-------------+---------------------------------+--+--+--+
还有一个:

Return manufacturers with more than 200 planes:

SELECT COUNT(tailnum) AS num_planes, manufacturer 
FROM planes 
GROUP BY manufacturer 
HAVING COUNT(*) > 200 
ORDER BY num_planes DESC
这将返回:

+------------+--------------------+
| num_planes | manufacturer       |
+------------+--------------------+
| 1630       | "BOEING"           |
+------------+--------------------+
| 400        | "AIRBUS INDUSTRIE" |
+------------+--------------------+
| 368        | "BOMBARDIER INC"   |
+------------+--------------------+
| 336        | "AIRBUS"           |
+------------+--------------------+
| 299        | "EMBRAER"          |
+------------+--------------------+
现在,我试图查询每个拥有200多架飞机的制造商的航班数量

编写了以下查询:

SELECT COUNT(flight) AS num_flights, pl.manufacturer
FROM flights fl, planes pl JOIN
(SELECT COUNT(tailnum) AS num_planes, pl2.manufacturer
    FROM planes pl2
    GROUP BY pl2.manufacturer 
    HAVING COUNT(*) > 200
    ORDER BY num_planes DESC) tm
    ON pl.manufacturer = tm.manufacturer
GROUP BY pl.manufacturer
ORDER BY num_flights DESC
但是,此查询返回的航班数不正确,并且需要花费很长时间才能执行:

+-------------+--------------------+
| num_flights | manufacturer       |
+-------------+--------------------+
| 262029020   | "BOEING"           |
+-------------+--------------------+
| 64301600    | "AIRBUS INDUSTRIE" |
+-------------+--------------------+
| 59157472    | "BOMBARDIER INC"   |
+-------------+--------------------+
| 54013344    | "AIRBUS"           |
+-------------+--------------------+
| 48065446    | "EMBRAER"          |
+-------------+--------------------+
我做错了什么

表结构:

planes:

CREATE TABLE planes
(
     tailnum VARCHAR(6),
     manufacturer VARCHAR(50)
)

+----------+--------------------+
| tailnum  | manufacturer       |
+----------+--------------------+
| "N10156" | "EMBRAER"          |
+----------+--------------------+
| "N102UW" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| "N103US" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| "N104UW" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| "N10575" | "EMBRAER"          |
+----------+--------------------+
| "N105UW" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| "N107US" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| ...      | ...                |
+----------+--------------------+

flights:

CREATE TABLE flights
(
     flight INT,
     tailnum VARCHAR(6)
)

+--------+----------+
| flight | tailnum  |
+--------+----------+
| 1545   | "N14228" |
+--------+----------+
| 1714   | "N24211" |
+--------+----------+
| 1141   | "N619AA" |
+--------+----------+
| 461    | "N668DN" |
+--------+----------+
| 1696   | "N39463" |
+--------+----------+
| ...    | ...      |
+--------+----------+

您可以尝试按tailnum连接飞机和航班,按制造商计算tailnum和航班分组,并通过在COUNTtailnum上添加子句进行过滤

选择制造商作为制造商, COUNTDISTINCT tailnum作为平面数, 将航班数作为航班数, 从飞机上 内部连接航班 在飞机上。tailnum=flights.tailnum 按制造商分组 计数大于200的 3点前订购
为什么选择使用古语语法而不是正确、明确、标准、可读的联接语法?@GordonLinoff查询的哪部分不可读?你能给我举个例子,说明一个正确的、明确的、标准的、可读的连接语法是什么样子的吗?在最终的查询中,航班和飞机之间没有连接条件,因此导致了交叉连接。如果您按照Gordon的建议切换到JOIN语法,您会注意到,因为您会收到一条关于缺少的ONIT的错误消息,所以最好也共享您的表结构。您能用文本数据替换图像吗?这将有助于与StackOverflow行为准则进行比较和比较。谢谢!但再仔细检查一下,您的查询似乎返回了200多个航班的制造商?我需要的是每个拥有200多架飞机的制造商的航班数量。因此,输出应该只包含5行。请参见查询2输出-现在它返回查询1中前8行的8行。我已经更新了答案,修改了选择。如果我理解的话,你可以在不止一次飞行中拥有同一架飞机。因此,您需要输入DISTINCT,以便为制造商计数,只需输入tailnum中的DISTINCT值。希望这对你有帮助。