我也来散分

jyxhz 2007-05-16 09:29:33
有表table(cid,gid),表示(客户代码,物品代码)
c1,g1
c1,g2
c2,g2
c2,g3
....


求至少购买了c1客户购买的所有物品的客户号

写出一种方法给10分,多多益善
...全文
192 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
江城老温 2007-05-17
  • 打赏
  • 举报
回复
select * from 客户表
where not exists(
select tmp.gid from [table] as tmp where tmp.cid='c1'
and not exists(select 1 from [table] where cid=客户表.cid and gid=tmp.gid)
)
wgzaaa 2007-05-16
  • 打赏
  • 举报
回复
select distinct gid from tb A where exists(select 1 from tb B where A.gid=B.gid and B.Cid='c1')
wgzaaa 2007-05-16
  • 打赏
  • 举报
回复
select distinct gid from tb where gid in(select gid from tb where cid='c1' )
wgzaaa 2007-05-16
  • 打赏
  • 举报
回复
select * from tb where gid in(select gid from tb where cid='c1' )
jyxhz 2007-05-16
  • 打赏
  • 举报
回复
上面都行,还有吗?
lzhcxc 2007-05-16
  • 打赏
  • 举报
回复
--借用二楼的数据


create table ta(cid varchar(10), [name] varchar(10))
insert ta select 'c1','AA'
union all select 'c2','BB'
union all select 'c3','CC'
union all select 'c4','DD'

create table tb(cid varchar(10),gid varchar(10))
insert into tb(cid,gid) values('c1','g1')
insert into tb(cid,gid) values('c1','g2')
insert into tb(cid,gid) values('c2','g1')
insert into tb(cid,gid) values('c2','g2')
insert into tb(cid,gid) values('c2','g3')
insert into tb(cid,gid) values('c3','g1')
insert into tb(cid,gid) values('c4','g1')
insert into tb(cid,gid) values('c4','g2')
insert into tb(cid,gid) values('c4','g3')

select * from ta
where not exists(
select tmp.gid from [tb] as tmp where tmp.cid='c1'
and not exists(select 1 from [tb] where cid=ta.cid and gid=tmp.gid)
)

--result
cid name
---------- ----------
c1 AA
c2 BB
c4 DD

(3 row(s) affected)
lzhcxc 2007-05-16
  • 打赏
  • 举报
回复
--try


select * from 客户表
where not exists(
select tmp.gid from [table] as tmp where tmp.cid='c1'
and not exists(select 1 from [table] where cid=客户表.cid and gid=tmp.gid)
)
dawugui 2007-05-16
  • 打赏
  • 举报
回复
if object_id('pubs..tb') is not null
drop table tb
go

create table tb(cid varchar(10),gid varchar(10))
insert into tb(cid,gid) values('c1','g1')
insert into tb(cid,gid) values('c1','g2')
insert into tb(cid,gid) values('c2','g1')
insert into tb(cid,gid) values('c2','g2')
insert into tb(cid,gid) values('c2','g3')
insert into tb(cid,gid) values('c3','g1')
insert into tb(cid,gid) values('c4','g1')
insert into tb(cid,gid) values('c4','g2')
insert into tb(cid,gid) values('c4','g3')
go
select cid from
(
select * from tb where cid <> 'c1' and gid in (select gid from tb where cid = 'c1')
) t
group by cid
having count(*) >= (select count(*) from (select gid from tb where cid = 'c1') m)

drop table tb

/*
cid
----------
c2
c4

(所影响的行数为 2 行)

*/
dawugui 2007-05-16
  • 打赏
  • 举报
回复
select cid , count(*) from
(
select * from tb where cid <> 'c1' and gid in (select gid from tb where cid = 'c1')
) t
group by cid
having count(*) >= select count(*) from (select gid from tb where cid = 'c1') m

34,576

社区成员

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

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