内部连接和左连接使用 where 过滤器给出相同的结果,因此使用哪个连接?

Inner join and left join gives same results with where filter and hence which join to use?

我想获取所有订购粉色的客户

客户table

cid      name
1001     Anna
1002     Boi
1003     Canny
1004     Dore

订单Table

cid   color
1001  pink
1002  yellow
1005  green
1003  pink

我尝试像下面这样使用左连接

select t1.cid,t1.name,t2.color
from Customers t1
left join Orders t2
on t1.cid=t2.cid
where t2.color='pink'

我尝试使用内连接

 select t1.cid,t1.name,t2.color
 from Customers t1
 inner join Orders t2
 on t1.cid=t2.cid
 where t2.color='pink'

使用内连接或左连接,我得到与上述查询相同的结果。 我了解这 2 个连接是如何工作的。内部联接将仅从 table 中获取匹配的记录并应用 where 过滤器。左连接将从左 table 获取所有记录,然后应用 where 过滤器。 但是我很困惑在上面这样的情况下使用哪个。

有很多这样的场景,我对使用哪个连接感到困惑,即使我知道如何获得所需的结果。我对 SQL 有点陌生,是否有人可以帮助我?

当您在要加入的 table 上应用过滤器(where 子句条件)时会发生这种情况。在这种情况下 'Order' table.

这是因为您的 WHERE 子句明确过滤了订单 table 中颜色为粉红色的行。然后它将仅加入 Customer table.

上匹配的订单行

您会发现,当您删除 where 子句时,左联接将按您预期的方式运行。 :)