这个SQL语句如何写?

brightview 2003-09-23 03:55:56
有两个表A B
A表:
a_id 成员
-------------
1 张三,李四
2 王五、赵六
.....
B表

b_id 姓名
-------------
1 张三
2 李四
3 王五
....


现想分类汇总B中人在A中的出现次数

我写的如下:
select 成员,count(成员) as 出现的次数
from a
where 成员 like '%这里不知道怎么写了%'
group by 成员

...全文
34 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
playyuer 2003-11-10
  • 打赏
  • 举报
回复
up
playyuer 2003-09-28
  • 打赏
  • 举报
回复
快给粉!
快结帐!
yangvxin1 2003-09-23
  • 打赏
  • 举报
回复
playyuer 厉害。
playyuer 2003-09-23
  • 打赏
  • 举报
回复
A:
a_id 成员
----------- ----------------------------------
1 张三,李四
2 李四王五、赵六李四王五赵六赵六
3 李四王五李四王五李四王五、张三赵六
4 王五、张三赵六

B:
b_id 姓名
----------- ----
1 张三
2 李四
3 王五
4 赵六

--========================

select b.姓名, sum((len(a.成员) - len(replace(a.成员,b.姓名,'')))/len(b.姓名))
from b ,a
where charindex(b.姓名,a.成员)>0
group by b.姓名


select b.姓名, sum((len(a.成员) - len(replace(a.成员,b.姓名,'')))/len(b.姓名))
from b left join a on charindex(b.姓名,a.成员)>0
group by b.姓名
prairie_orchid 2003-09-23
  • 打赏
  • 举报
回复
select cc.d,count(cc.b) from (
select aa.b b,bb.d d
from(
select 成员 b, 姓名 d from A,B where instr(成员,姓名)>0) aa,B bb
where aa.d(+)=bb.d) cc
group by cc.d

oracle 数据库的语法,不知道SQL Server 能不能用
yujohny 2003-09-23
  • 打赏
  • 举报
回复
declare @A table(a_id int,成员 varchar(20))
insert into @a
select 1,'张三,李四'
union all select 2,'王五,赵六,张三'

declare @B table(b_id int,姓名 varchar(10))
insert into @b
select 1,'张三'
union all select 2,'李四'
union all select 3,'王五'

select b.姓名,count(*) as 出现的次数
from @A a inner join @B b on ','+a.成员+',' like '%,'+b.姓名+',%'
group by b.姓名
yujohny 2003-09-23
  • 打赏
  • 举报
回复
select a.成员,count(a.成员) as 出现的次数
from a inner join b on ','+a.成员+',' like '%,'+b.姓名+',%'
group by a.成员
zjcxc 元老 2003-09-23
  • 打赏
  • 举报
回复
下面是数据测试:

declare @A table(a_id int,成员 varchar(20))
insert into @a
select 1,'张三,李四,张三'
union all select 2,'王五,赵六,张三'

declare @B table(b_id int,姓名 varchar(10))
insert into @b
select 1,'张三'
union all select 2,'李四'
union all select 3,'王五'

--创建数据分拆的临时表
select top 8000 id=identity(int,1,1) into #tb
from(select top 100 id from syscolumns) a,
(select top 100 id from syscolumns) b,
(select top 100 id from syscolumns) c

select a.姓名,次数=sum(case when b.姓名 is null then 0 else 1 end)
from @B a left join
(
select 姓名=substring(成员,b.id,charindex(',',成员+',',b.id)-b.id)
from @a a,#tb b
where substring(','+成员,b.id,1)=','
) b on a.姓名=b.姓名 group by a.姓名

--删除数据处理临时表
drop table #tb
zjcxc 元老 2003-09-23
  • 打赏
  • 举报
回复
如果你的姓名在A表的每条记录中只出现一次,马可的方法是可以的.

如果不是的话,需要用下面的处理方法.
--创建数据分拆的临时表
select top 8000 id=identity(int,1,1) into #tb
from(select top 100 id from syscolumns) a,
(select top 100 id from syscolumns) b,
(select top 100 id from syscolumns) c

--得到结果
select a.姓名,次数=sum(case when b.姓名 is null then 0 else 1 end)
from B a left join
(
select 姓名=substring(成员,b.id,charindex(',',成员+',',b.id)-b.id)
from A a,#tb b
where substring(','+成员,b.id,1)=','
) b on a.姓名=b.姓名 group by a.姓名

--删除临时表
drop table #tb
brightview 2003-09-23
  • 打赏
  • 举报
回复
自己搞定了。先建立一个视图,然后从视图中得到了结果
brightview 2003-09-23
  • 打赏
  • 举报
回复
马可的方法失败,望各路高手不吝赐教
zgwan2000 2003-09-23
  • 打赏
  • 举报
回复
只有做个游标才行哦
txlicenhe 2003-09-23
  • 打赏
  • 举报
回复
select 姓名,
(Select count(*) from A表 where charindex(b.姓名,成员)>0) as 出现的次数
from B表 b

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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