左连接返回左表中的行,并且对于左表中的每一行,右表中的所有匹配行。
例如:
create table Customers (name varchar(50));
insert Customers values
('Tim'),
('John'),
('Spike');
create table Orders (customer_name varchar(50), product varchar(50));
insert Orders values (
('Tim', 'Guitar'),
('John', 'Drums'),
('John', 'Trumpet');
create table Addresses (customer_name varchar(50), address varchar(50));
insert Addresses values (
('Tim', 'Penny Lane 1'),
('John', 'Abbey Road 1'),
('John', 'Abbey Road 2');
然后,如果您运行:
select c.name
, count(o.product) as Products
, count(a.address) as Addresses
from Customers c
left join Orders o on o.customer_name = c.name
left join Addresses a on a.customer_name = c.name
group by name
你得到:
name Products Addresses
Tim 1 1
John 4 4
Spike 0 0
但是 John 没有 4 个产品!
如果您在没有 的情况下运行group by
,您可以看到计数关闭的原因:
select *
from Customers c
left join Orders o on o.customer_name = c.name
left join Addresses a on a.customer_name = c.name
你得到:
name customer_name product customer_name address
Tim Tim Guitar Tim Penny Lane 1
John John Drums John Abbey Road 1
John John Drums John Abbey Road 2
John John Trumpet John Abbey Road 1
John John Trumpet John Abbey Road 2
Spike NULL NULL NULL NULL
如您所见,连接最终会相互重复。对于每个产品,地址列表都是重复的。这会给你错误的计数。要解决此问题,请使用其他出色的答案之一:
select c.name
, o.order_count
, a.address_count
from Customers c
left join
(
select customer_name
, count(*) as order_count
from Orders
group by
customer_name
) o
on o.customer_name = c.name
left join
(
select customer_name
, count(*) as address_count
from Addresses
group by
customer_name
) a
on a.customer_name = c.name
子查询确保每个客户只加入一行。结果要好得多:
name order_count address_count
Tim 1 1
John 2 2
Spike NULL NULL