查询两个表,根据A表ID字段查出B表有A表记录个数

wn520 2009-10-15 11:26:20
如题
查询两个表,根据A表ID字段查出B表有A表记录个数
...全文
108 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
wn520 2009-10-15
  • 打赏
  • 举报
回复
谢谢两位了!都可以了!
那如果涉及到3个表,像下面的语句该怎样写咧!
麻烦了!
[Quote=引用 11 楼 sql77 的回复:]
SQL codeselect a1.id,count(b1.id)from aas a1join bas b1on a.id=b.idgroupby A1.id
[/Quote]
SQL77 2009-10-15
  • 打赏
  • 举报
回复
select a1.NAME,count(b1.id) from a as a1 
join b as b1 on a.id =b.id
group by A1.NAME

我的也修改呀,哎,是要NAME
--小F-- 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 sql77 的回复:]
引用 12 楼 fredrickhu 的回复:
SQL code---写掉条件了select
  A.name,count(1)as numfrom
  a,bwhereexists(select1from awhere id=a.id)and a.id=b.id

小F,加个分组
[/Quote]

呵呵 谢谢 写掉了
select 
A.name,count(1) as num
from
a,b
where
exists(select 1 from a where id=a.id) and a.id=b.id
group by
A.name
SQL77 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 fredrickhu 的回复:]
SQL code---写掉条件了select
A.name,count(1)as numfrom
a,bwhereexists(select1from awhere id=a.id)and a.id=b.id
[/Quote]
小F,加个分组
--小F-- 2009-10-15
  • 打赏
  • 举报
回复
---写掉条件了
select
A.name,count(1) as num
from
a,b
where
exists(select 1 from a where id=a.id) and a.id=b.id
SQL77 2009-10-15
  • 打赏
  • 举报
回复
select a1.id,count(b1.id) from a as a1 
join b as b1 on a.id =b.id
group by A1.id
SQL77 2009-10-15
  • 打赏
  • 举报
回复
select a1.id,count(b1.id) from a as a1 
join b as b1 on a.id =b.id
group by b1.id


就直接查相等的,就这样
wn520 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fredrickhu 的回复:]
SQL codeselect A.name,count(1)as numfrom bwhereexists(select1from awhere id=a.id)
[/Quote]


还是不行呀

报这样的错误 “列前缀 'A' 与查询中所用的表名或别名不匹配。”
wn520 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wn520 的回复:]
  我也需要 A表的字段

详细说明下:

A表        B表
  ID        ID
  NAME      AID

查询 字段 A表的NAME字段,B表 统一相应A表记录条数
[/Quote]

这样还不清楚吗?
goodsss 2009-10-15
  • 打赏
  • 举报
回复
select a1.id,count(b1.id) from a as a1
left join b as b1 on a.id =b.id
group by b1.id having count(b1.id)>=0 ;

你没有把需求说得太清晰,你试试这样行吗?
祝你好运.
--小F-- 2009-10-15
  • 打赏
  • 举报
回复
select A.name,count(1) as num from b where exists(select 1 from a where id=a.id)
wn520 2009-10-15
  • 打赏
  • 举报
回复
我也需要 A表的字段

详细说明下:

A表 B表
ID ID
NAME AID

查询 字段 A表的NAME字段,B表 统一相应A表记录条数
--小F-- 2009-10-15
  • 打赏
  • 举报
回复
select count(1) as num from b where exists(select 1 from a where id=a.id)
luoyoumou 2009-10-15
  • 打赏
  • 举报
回复
select count(*) from B where id in ( seelct Id from a);
luoyoumou 2009-10-15
  • 打赏
  • 举报
回复
scount count(*) from B where id in ( seelct Id from a);
luoyoumou 2009-10-15
  • 打赏
  • 举报
回复
scount (*) from B where id in ( seelct Id from a);
wn520 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 sql77 的回复:]
引用 16 楼 wn520 的回复:
谢谢两位了!都可以了!
那如果涉及到3个表,像下面的语句该怎样写咧!
麻烦了!
引用 11 楼 sql77 的回复:
SQL codeselect a1.id,count(b1.id)from aas a1join bas b1on a.id=b.idgroupby A1.id


类似的,看条件连接
[/Quote]

谢谢了! 结贴了!
SQL77 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 wn520 的回复:]
谢谢两位了!都可以了!
那如果涉及到3个表,像下面的语句该怎样写咧!
麻烦了!
引用 11 楼 sql77 的回复:
SQL codeselect a1.id,count(b1.id)from aas a1join bas b1on a.id=b.idgroupby A1.id

[/Quote]
类似的,看条件连接

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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