gpt4 book ai didi

postgresql - PostgreSQL 的这两个查询有什么区别?

转载 作者:行者123 更新时间:2023-11-29 13:45:35 25 4
gpt4 key购买 nike

这 2 个查询在相同的条件下获取相同的列。但结果仍然不同:

首先使用连接:

select 
s.codice as codice,
n.ragione_sociale as ragione_sociale
from
parco_veicoli pv
right join
noleggiatori n on pv.id_noleggiatore = n.id
right join
sedi s on pv.id_sede = s.id
order by codice, ragione_sociale;

第一个查询给出总共 1323 行的输出。

没有连接的第二个:

SELECT 
s.codice as codice,
n.ragione_sociale as ragione_sociale
FROM
parco_veicoli pv,
noleggiatori n,
sedi s
WHERE
pv.id_sede = s.id AND
pv.id_noleggiatore = n.id
order by codice, ragione_sociale;

第二个查询给出总共 1321 行的输出。

问题:

谁能告诉我这两个查询的含义相同还是不同?

因为输出几乎相同,但第二个查询的结果集(行)比第一个查询少。

最佳答案

https://www.postgresql.org/docs/current/static/queries-table-expressions.html

FROM T1 CROSS JOIN T2 is equivalent to FROM T1 INNER JOIN T2 ON TRUE (see below). It is also equivalent to FROM T1, T2.

RIGHT OUTER JOIN

First, an inner join is performed. Then, for each row in T2 that does not satisfy the join condition with any row in T1, a joined row is added with null values in columns of T1. This is the converse of a left join: the result table will always have a row for each row in T2.

所以在您的查询中,第二个是 INNER join,第一个是 RIGHT OUTER JOIN

关于postgresql - PostgreSQL 的这两个查询有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48919928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com