表1
id name
--1--aa
--2--bb
--3--cc
--4--dd
--5--ff
--6--gg
表2
id-----(表1的外键)表1.id-----(和当前表1.id对应的表的id)id
1------1-------2
2------1-------3
3------1-------4
4------2-------1
5------2-------2
6------3-------1
7------4-------2查出来的效果----id----name----count
1-------aa--------3
2-------bb--------2
3-------cc--------1
4-------dd--------1
5-------ff--------0
6-------gg--------0
id name
--1--aa
--2--bb
--3--cc
--4--dd
--5--ff
--6--gg
表2
id-----(表1的外键)表1.id-----(和当前表1.id对应的表的id)id
1------1-------2
2------1-------3
3------1-------4
4------2-------1
5------2-------2
6------3-------1
7------4-------2查出来的效果----id----name----count
1-------aa--------3
2-------bb--------2
3-------cc--------1
4-------dd--------1
5-------ff--------0
6-------gg--------0
from tb1 left join tb2 on tb1.id=tb2.id
group by tb1.id,name
a.*,
isnull((select count(*) from 表2 where [表1.id]=表1.id),0)
from 表1
表1
id name
--1--aa
--2--bb
--3--cc
--4--dd
--5--ff
--6--gg
表2
id-----(表1的外键)表1.id-----(和当前表1.id对应的表的id)id
1------1-------2
2------1-------3
3------1-------4
4------2-------1
5------2-------2
6------3-------1
7------4-------2 查出来的效果 ----id----name----count
1-------aa--------3
2-------bb--------2
3-------cc--------1
4-------dd--------1
5-------ff--------0
6-------gg--------0 这个查出来怎么是
----id----name----count
1-------aa--------3
2-------bb--------2
3-------cc--------1
4-------dd--------1
5-------ff--------1
6-------gg--------1ff和gg明显是0请修改下?
*,
isnull((select count(*) from 表2 where [表1.id]=表1.id),0)
from 表1 /**
id name
----------- ---- -----------
1 aa 3
2 bb 2
3 cc 1
4 dd 1
5 ff 0
6 gg 0(所影响的行数为 6 行)
**/a
declare @t table(id int,name nvarchar(10))insert @t select 1,'aa'
insert @t select 2,'bb'
insert @t select 3,'cc'
insert @t select 4,'dd'
insert @t select 5,'ff'
insert @t select 6,'gg'declare @t1 table(id1 int,id2 int,id3 int)
insert @t1 select 1,1,2
insert @t1 select 2,1,3
insert @t1 select 3,1,4
insert @t1 select 4,2,1
insert @t1 select 5,2,2
insert @t1 select 6,3,1
insert @t1 select 7,4,2 select A.[id],A.[name], count(A.[id])[count]
from @t A
left join @t1 B
on A.[id]=B.[id2]
where B.[id1] is not null
group by A.[id],A.[name]
union
select A.[id],A.[name], 0
from @t A
left join @t1 B
on A.[id]=B.[id2]
where B.[id1] is null
--结果-----------------------1 aa 3
2 bb 2
3 cc 1
4 dd 1
5 ff 0
6 gg 0
*,
isnull((select count(*) from 表2 where [表1.id]=表1.id),0)
from 表1 where [表1.id]=表1.id 是什么意思啊?中括号?
isnull是连在一起的吗?
中括号里面的是一个字段名isnull(col,0) 如果这个数为空,则置0
create table T1(id int,name nvarchar(10))insert T1 select 1,'aa'
insert T1 select 2,'bb'
insert T1 select 3,'cc'
insert T1 select 4,'dd'
insert T1 select 5,'ff'
insert T1 select 6,'gg'create table T2(id1 int,id2 int,id3 int)
insert T2 select 1,1,2
insert T2 select 2,1,3
insert T2 select 3,1,4
insert T2 select 4,2,1
insert T2 select 5,2,2
insert T2 select 6,3,1
insert T2 select 7,4,2 -------------------------------
select*,
(select count(*) from T2 where T1.[id]=T2.id2)[count]
from T1
------------------------------------
1 aa 3
2 bb 2
3 cc 1
4 dd 1
5 ff 0
6 gg 0
left join
(select aid, count(*) as num from 表2 group by aid) b
on a.id=b.aid