各位高手,100分求教

f190 2003-04-30 10:31:26
共有三个表

T1
------------------------
f1
------------------------
A
B
-------------------------


T2
-------------------------
f1 f2
-------------------------
A aa
A bb
B cc
B dd
-------------------------


T3
------------------------
f1 f2
------------------------
k1 aa
k1 bb
k2 cc
k2 ee
k3 dd
------------------------

T1,T2,T3是表名,f1,f2,f3是字段名
要求在T1中选出符合以下条件的f1:
它在T2中对应的f2所有的值,在T3中的f2都存在
如T1中的A,在T2中对应aa,bb,在T3中的k1有对应的aa,bb(按T3的f1分组),所以A被选出
而T1中的B,在T2中对应cc,dd,在T3中找不到对应的cc,dd,所以不选出。

说简单些,就是在一个表里的一组值,能在另外一张表中能找到完全匹配的一组值

不要求在一个SQL语句中完成,可以用临时表,不能用游标。谢谢!
...全文
28 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
f190 2003-05-06
  • 打赏
  • 举报
回复
谢谢各位高手!
joygxd()
happydreamer(偶很菜)
Quady515(柱子)
CrazyFor(蚂蚁)
的语句是正确的。遗憾蚂蚁兄的程序不不能用在Sybase上。

已结贴,祝工作愉快!
Quady515 2003-05-06
  • 打赏
  • 举报
回复
SELECT a.f1 ff1,b.f2 ff2,c.f1 ff3 INTO #TEMP1
FROM t1 a,t2 b,t3 c
WHERE a.f1 = b.f1 AND b.f2 = c.f2

SELECT DISTINCT c.fld1
FROM (SELECT a.ff1 fld1,a.ff3 fld2,COUNT(a.ff1) sum1 FROM #TEMP1 a GROUP BY a.ff1,a.ff3) c,
(SELECT b.f1 fld1,COUNT(b.f1) sum1 FROM T2 b GROUP BY b.f1) d
WHERE c.fld1 = d.fld1 AND c.sum1 >= d.sum1

DROP TABLE #TEMP1

以上语句确实只得到:A
楼主再试试.
CrazyFor 2003-04-30
  • 打赏
  • 举报
回复
select * from t1 where f1 in (
select a.f1 from
t1 a left join t2 b on a.f1=b.f1
left join t3 c on b.f2=c.f2
where b.f1 is not null and c.f2 is not null)temp
Quady515 2003-04-30
  • 打赏
  • 举报
回复
happydreamer(偶很菜)

你把 T3表中的最后一行改为

k3 de

试试,

A
B
happydreamer 2003-04-30
  • 打赏
  • 举报
回复
select a.t1 from #t2 a join #t3 b on a.num<=b.num and a.cid=b.cid

第一个错了 改为
select distinct f1,dbo.getresult(f1) result into #t2 from t2 --notice
select distinct f1,dbo.getresult1(f1) result into #t3 from t3
happydreamer 2003-04-30
  • 打赏
  • 举报
回复
select * from #t2 a join #t3 b on a.num<=b.num and a.cid=b.cid
Quady515 2003-04-30
  • 打赏
  • 举报
回复
a若T3中的数据为以下,
T3
------------------------
f1 f2
------------------------
k1 aa
k1 bb
k2 cc
k2 ee
k3 dd
------------------------

那么happydreamer(偶很菜)的第二种方法找出的是 B ,
不复合要求.
happydreamer 2003-04-30
  • 打赏
  • 举报
回复
第一个改为

select * from #t2 a , #t3 b
where charindex(a.result,b.result)>0

第二个我再想想
Quady515 2003-04-30
  • 打赏
  • 举报
回复
select a.f1 from #t2 a join #t3 b on a.num<=b.num and a.cid=b.cid

????
Quady515 2003-04-30
  • 打赏
  • 举报
回复
如果在T3中再加一条记录
f1 f2
----------------
k1 dd

怎么办?
zhangning163 2003-04-30
  • 打赏
  • 举报
回复
select a.f1 from T1 a where a.f1 in (select DISTINCT b.f1 from T2 b where b.f2 in
(select DISTINCT c.f2 from T3 c ));
joygxd 2003-04-30
  • 打赏
  • 举报
回复
select identity(int,1,1) cid,
a.f1,
count(a.f1) num
into #t2
from t2 a join t3 b on a.f2=b.f2
group by a.f1

select identity(int,1,1) cid,
b.f1,
count(b.f1) num
into #t3
from t2 a join t3 b on a.f2=b.f2
group by b.f1

select a.f1 from #t2 a join #t3 b on a.num=b.num and a.cid=b.cid ,t1 where t1.f1=a.f1
drop table #t2
drop table #t3
qihuhu 2003-04-30
  • 打赏
  • 举报
回复
真是太难了。
happydreamer 2003-04-30
  • 打赏
  • 举报
回复
你再出给点数据偶来试试看
Quady515 2003-04-30
  • 打赏
  • 举报
回复
happydreamer(偶很菜) ( )
的第二方案刚好能得到正确的数据,但不正确.

研究中.....
happydreamer 2003-04-30
  • 打赏
  • 举报
回复
or

select identity(int,1,1) cid,
a.f1,count(a.f1) num into #t2 from t2 a join t3 b on a.f2=b.f2
group by a.f1

select identity(int,1,1) cid,
b.f1,count(b.f1) num into #t3 from t2 a join t3 b on a.f2=b.f2
group by b.f1

select a.f1 from #t2 a join #t3 b on a.num=b.num and a.cid=b.cid

happydreamer 2003-04-30
  • 打赏
  • 举报
回复

create function getresult1(@f1 varchar(10))
returns varchar(100)
as
begin
declare @result varchar(100)
set @result=''
select @result=@result+' '+f2 from t3 where f1=@f1
return @result
end


create function getresult(@f1 varchar(10))
returns varchar(100)
as
begin
declare @result varchar(100)
set @result=''
select @result=@result+' '+f2 from t2 where f1=@f1
return @result
end


select distinct f1,dbo.getresult1(f1) result into #t2 from t2
select distinct f1,dbo.getresult1(f1) result into #t3 from t3

select * from #t2 a join #t3 b on a.result=b.result
Rotaxe 2003-04-30
  • 打赏
  • 举报
回复
select f1 from t1 where t1 in( select f1 from t2 inner join t3 on t2.f2=t3.f2)
joygxd 2003-04-30
  • 打赏
  • 举报
回复
不需要关联,只要所得的t2.f2数据在t3并且t3.f1相同就可以了
我再看看
ny64 2003-04-30
  • 打赏
  • 举报
回复
表T2、T3有关联吗?
加载更多回复(14)

22,209

社区成员

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

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