我不知道你的数据结构,但我认为你想要做的是:
create table Chapters (Chapter int);
insert Chapters values (1);
insert Chapters values (2);
insert Chapters values (3);
create table Words (Word varchar(50));
insert into Words values ('dog');
insert into Words values ('cat');
create table Chapters_Words (Word varchar(50), Chapter int, [Count] int);
insert into Chapters_Words values ('dog', 1, 3);
insert into Chapters_Words values ('dog', 2, 7);
insert into Chapters_Words values ('dog', 3, 1);
insert into Chapters_Words values ('cat', 2, 4);
select
f.Word,
f.Chapter,
isnull(w.[Count], 0) [Count]
from
Chapters_Words w
right join (
select w.Word, c.Chapter
from Chapters c
cross join Words w
) f on f.Chapter = w.Chapter and f.Word = w.Word
结果:
Word Chapter Count
-------------------------------------------------- ----------- -----------
dog 1 3
dog 2 7
dog 3 1
cat 1 0
cat 2 4
cat 3 0