不难的sql,但没想出来.望大家吝赐教

shuai45 2007-12-08 12:28:11
表A
id sign name otid
1 1 a 1
2 2 b 1
3 1 c 1
4 1 d 1
5 3 e 1
6 4 f 3

表B

id sign2 name
1 1 a
2 2 b
3 2 c
4 1 d
5 1 f
6 5 g
A.sign 与B.sign2关联.希望取出 A.sign =B.sign2 and A.sign=2 and A.otid=1的所有数据和数量
最后结果 应该为

A.id A.name num
1 a 3
2 b 2
5 e 1
...全文
123 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
JiangHongTao 2007-12-26
  • 打赏
  • 举报
回复
create table #t(id int,name varchar(10),otid int,sign int)
insert #t select 1,'a',0,1
union all select 2,'b',1,2
union all select 3,'c',1,2
union all select 4,'d',1,3
select a.id,a.name,b.sum from #t a,(
select otid,count(*) sum from #t where otid in(
select id from #t where sign in(1,2))
group by otid
) b
where a.id = b.otid

drop table #t
Limpire 2007-12-08
  • 打赏
  • 举报
回复
A.sign =B.sign2
匹配结果根本没有A.id=5的纪录
Limpire 2007-12-08
  • 打赏
  • 举报
回复
莫名其妙
fa_ge 2007-12-08
  • 打赏
  • 举报
回复
我相信楼主自己可以写出来
fa_ge 2007-12-08
  • 打赏
  • 举报
回复
sf,我去吃饭了,等下回来
pt1314917 2007-12-08
  • 打赏
  • 举报
回复
貌似楼主的意思是要取出节点下的所有子节点
哎,楼主问题都描述不清楚。
pt1314917 2007-12-08
  • 打赏
  • 举报
回复
貌似楼主的意思是要取出节点下的所有子节点
哎,建议楼主先去学学语文吧`
yowbell 2007-12-08
  • 打赏
  • 举报
回复
帮顶,应该不难吧
shuai45 2007-12-08
  • 打赏
  • 举报
回复
是我没讲明白,不怪大家。
是这样:
id name otid sign
1 a 0 1
2 b 1 2
3 c 1 2
4 d 1 3
希望找出 id与otid关联的 切sign=1(1带表一级分类)或sign=2(2代表二级分类)。。。。的数据和数量
例如:找出id=otid and sign=1
id nane num
1 a 3

例如:找出id=otid and sign=2
id nane num
1 a 3
(找出2级以下的所有 id=otid 的数据和数量)
不知道我说明白了没
shuai45 2007-12-08
  • 打赏
  • 举报
回复
每人回答?
fa_ge 2007-12-08
  • 打赏
  • 举报
回复
楼主,你的结果的是有问题的吧
OracleRoob 2007-12-08
  • 打赏
  • 举报
回复
你给的数据不对吧?

22,298

社区成员

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

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