查询两表关联查询个数,帮帮我吧

benison77 2008-09-18 11:59:10
表一:id 字段

表二:id 字段 data 字段

(表2中 id 字段的数据 表一 id 必须有 且可重复)

查出(表一中 id 字段) 和 (表一 id 字段与表二中 id 字段相等的个数 (包括 0) )


头疼了我好几天了,谁能帮帮我

...全文
81 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
benison77 2008-09-18
  • 打赏
  • 举报
回复
非常感谢楼上的各位
hyqwan11112 2008-09-18
  • 打赏
  • 举报
回复

--左连接
declare @表一 table(id nvarchar(20))
insert into @表一
select '01' union all
select '02' union all
select '03'
declare @表二 table(id nvarchar(20),data nvarchar(20))
insert into @表二
select '01','f' union all
select '01','b' union all
select '01','c' union all
select '02','a'

select a.id,count(data) as count
from @表一 a left join @表二 b
on a.id = b.id
group by a.id
-晴天 2008-09-18
  • 打赏
  • 举报
回复
create table table1(id varchar(10)) 
insert table1 select '01' union select '02' union select '03'

create table table2(id varchar(10),date varchar(10))
insert table2 select '01','b'
insert table2 select '01','c'
insert table2 select '02','a'
select id,isnull((select count(*) from table2 where id=a.id),0) as count from table1 a
go
drop table table1,table2
/*
id count
---------- -----------
01 2
02 1
03 0

(3 行受影响)
*/
-晴天 2008-09-18
  • 打赏
  • 举报
回复
select id,isnull((select count(*) from table2 where id=a.id),0) as count from table1
hyde100 2008-09-18
  • 打赏
  • 举报
回复

select
表一.id,
count(表二.id) row_count
from 表一
left outer join 表二 on 表一.id = 表二.id
group by 表一.id
wgzaaa 2008-09-18
  • 打赏
  • 举报
回复

create table 表一(id varchar(10))
insert 表一 select '01' union select '02' union select '03'
------------
create table 表二(id varchar(10),date varchar(10))
insert 表二 select '01','b'
insert 表二 select '01','c'
insert 表二 select '02','a'
--------------------------------
select a.id,count(b.id) [count] from 表一 a left join 表二 b on a.id=b.id group by a.id
---------------------------------
id count
01 2
02 1
03 0
Garnett_KG 2008-09-18
  • 打赏
  • 举报
回复


SELECT a.ID,COUNT(b.ID)
FROM
(
SELECT DISTINCT ID
FROM Table1
) a
LEFT JOIN
(
SELECT ID FROM Table2
) b
ON a.ID=b.ID
GROUP BY a.ID

benison77 2008-09-18
  • 打赏
  • 举报
回复
表一 id
01
02
03

表二 id data
01 f
01 b
01 c
02 a

要查询的结果

id count
01 3
02 1
03 0
dawugui 2008-09-18
  • 打赏
  • 举报
回复
select m.id , isnull(n.cnt , 0 ) cnt from tb1 m
left join
(select id , count(*) cnt from tb2 group by id) n
on m.id = n.id
dawugui 2008-09-18
  • 打赏
  • 举报
回复
select m.id , isnull(n.cnt , 0 ) cnt from tb1 m
left join
(select id , count(*) cnt from tb2 group by id) tb2
on m.id = n.id
wgzaaa 2008-09-18
  • 打赏
  • 举报
回复
select a.id,count(b.id) 相等的个数 from 表一 a left join 表二 b on a.id=b.id group by a.id
dawugui 2008-09-18
  • 打赏
  • 举报
回复
查出(表一中 id 字段) 和 (表一 id 字段与表二中 id 字段相等的个数 (包括 0) ) ???

好难懂.
CN_SQL 2008-09-18
  • 打赏
  • 举报
回复
描述得有点....,给点数据来吧.
-晴天 2008-09-18
  • 打赏
  • 举报
回复
且可重复:是在表一中可重复,还是在表二中可重复?
相等的个数:什么个数?有几种?还是每个id有几个相等?
zjcxc 2008-09-18
  • 打赏
  • 举报
回复
SELECT
A.id,
cnt = SUM(1)
FROM 表一 A
LEFT JOIN 表二 B
ON A.id = B.id
GROUP BY A.id

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧