左联接不返回左表中的所有记录

时间:2020-08-04 13:27:50

标签: sql sql-server

我想通过对两个表执行join来获得计数,我正在执行group byleft join

但是,如果该记录与另一个表不匹配,我就不会得到所有行。

在给定查询中:

create table UserTable (
  Id integer not null,
  Name varchar(12) not null
);

insert into UserTable  values (1,  'A B');
insert into UserTable  values (2,  'A C');
insert into UserTable  values (3,  'A C A C');
insert into UserTable  values (4,  'A C C');
insert into UserTable  values (5,  'A C B');
insert into UserTable  values (6,  'A C C');
insert into UserTable  values (7,  'A C D');
insert into UserTable  values (8,  'A C E');
insert into UserTable  values (9,  'A C F');


create table LogTable (
  LogId integer not null,
  Username varchar(12) not null,
  Event varchar(12) not null
);

insert into LogTable  values (1, 'A C A C', 'Read');
insert into LogTable  values (2, 'A C F', 'Write');
insert into LogTable  values (3, 'A C F', 'Read');
insert into LogTable  values (4, 'A C C', 'Update');
insert into LogTable  values (5,'A C C', 'Read');
insert into LogTable  values (6,'A C F', 'Read');
insert into LogTable  values (7,'A C F', 'Update');
insert into LogTable  values (7,'A C F', 'Write');
insert into LogTable  values (7,'A C E','Update');
insert into LogTable  values (7,'A C F', 'Delete');
insert into LogTable  values (10,'A C B', 'Delete');
insert into LogTable  values (11, 'A C F','Copy');
insert into LogTable  values (12, 'A C B','Read');
insert into LogTable  values (13, 'A C F','Update');
insert into LogTable  values (14, 'A C F','Copy');
insert into LogTable  values (15, 'A C F','Read');
insert into LogTable  values (16, 'A C F','Update');
insert into LogTable  values (17, 'A C F','Copy');
insert into LogTable  values (18, 'A C C','Read');
insert into LogTable  values (19, 'A C D','Update');

我正在做

SELECT COUNT(*) as Read,UT.Name, UT.Id 
FROM UserTable UT 
LEFT JOIN LogTable LT ON LT.Username = UT.Name 
WHERE LT.Event = 'Read'
GROUP by UT.Name, UT.Id

我想要从UserTable获取所有记录,如果LogTable中不存在该记录

如果不存在,则将ReadCount设为0。

为什么左联接在此查询中不返回 UserTable 的所有记录?

所需的输出:

Id  |   Name    |   ReadCount
-----------------------------
1   |   A B     |   0
2   |   A C     |   0
3   |   A C A C |   1
4   |   A C C   |   2
5   |   A C B   |   1
6   |   A C C   |   2
7   |   A C D   |   0
8   |   A C E   |   0
9   |   A C F   |   3

2 个答案:

答案 0 :(得分:2)

您需要将条件移至ON子句。但是,您不想使用COUNT(*),因为您想计算匹配数。那应该是:

SELECT UT.Name, UT.Id , COUNT(LT.UserName) as num_read
FROM UserTable UT LEFT JOIN
     LogTable LT
     ON LT.Username = UT.Name AND LT.Event = 'Read'
GROUP by UT.Name, UT.Id;

也就是说,使用相关子查询时,这种类型的查询通常会更快:

select ut.*,
       (select count(*)
        from logtable lt
        where lt.username = ut.name and lt.event = 'Read'
       ) as num_read
from usertable ut;

尤其是,这可以利用logtable(username, event)上的索引。

答案 1 :(得分:1)

SELECT COUNT(*) as Read,UT.Name, UT.Id 
FROM UserTable UT 
LEFT JOIN LogTable LT ON LT.Username = UT.Name 
WHERE LT.Event = 'Read'
GROUP by UT.Name, UT.Id

您使用了WHERE LT.Event = 'Read',这使其成为内部联接,这就是为什么您没有获得左表的所有行的原因

SELECT COUNT(*) as Read,UT.Name, UT.Id 
FROM UserTable UT 
LEFT JOIN LogTable LT ON LT.Username = UT.Name 
 and  LT.Event = 'Read'

GROUP by UT.Name, UT.Id

移动on子句中的条件