完全匹配问题

internetroot 2008-06-04 08:23:41
/*
完全匹配问题
测试数据
产品 厂家
---------------
a1 a
a2 a
a3 a
b1 b
b2 b
a1 c
a2 c
a3 c
b1 d
a1 d
a2 d
a3 d
e1 e
a1 s

输入参数('a1','a2','a3')应得到
a
c

输入('a1','a2')应没有返回值

输入('b1','b2')应返回
b

即输入一个产品列表返回刚好生产(不多生产一种也不少生产一种产品)这些产品的厂家

最好不要使用到group统计语句,且要考虑效率问题,谢谢!
*/
...全文
157 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyc_cheng 2008-06-04
  • 打赏
  • 举报
回复
写个函数通用,如果查询的参数变一次,写个select语句太死板了
cyc_cheng 2008-06-04
  • 打赏
  • 举报
回复
哈哈,写个函数,预设多个参数值,函数里面一个个select,不用group by

不过用group by简单省事啊,为啥不用呢
中国风 2008-06-04
  • 打赏
  • 举报
回复
以上写法都可以
--> --> (Roy_88)生成測試數據

if not object_id('T') is null
drop table T
Go
Create table T([产品] nvarchar(2),[厂家] nvarchar(1))
Insert T
select N'a1',N'a' union all
select N'a2',N'a' union all
select N'a3',N'a' union all
select N'b1',N'b' union all
select N'b2',N'b' union all
select N'a1',N'c' union all
select N'a2',N'c' union all
select N'a3',N'c' union all
select N'b1',N'd' union all
select N'a1',N'd' union all
select N'a2',N'd' union all
select N'a3',N'd' union all
select N'e1',N'e' union all
select N'a1',N's'
Go

print 'test1'
select [厂家]
from T a
where exists(select 1 from T where [厂家]=a.[厂家] and [产品] in('a1','a2','a3'))
group by [厂家]
having count(1)=(select count(distinct [产品]) from T where [产品] in('a1','a2','a3'))

print 'test2'

select [厂家]
from T a
where exists(select 1 from T where [厂家]=a.[厂家] and [产品] in('a1','a2'))
group by [厂家]
having count(1)=(select count(distinct [产品]) from T where [产品] in('a1','a2'))

print 'test3'

select [厂家]
from T a
where exists(select 1 from T where [厂家]=a.[厂家] and [产品] in('b1','b2'))
group by [厂家]
having count(1)=(select count(distinct [产品]) from T where [产品] in('b1','b2'))



(14 行受影响)
test1
厂家
----
a
c

(2 行受影响)

test2
厂家
----

(0 行受影响)

test3
厂家
----
b

(1 行受影响)
中国风 2008-06-04
  • 打赏
  • 举报
回复
select [厂家] 
from T a
where exists(select 1 from T where [厂家]=a.[厂家] and [产品] in('a1','a2','a3'))
group by [厂家]
having count(1)=(select count(distinct [产品]) from T where [产品] in('a1','a2','a3'))
中国风 2008-06-04
  • 打赏
  • 举报
回复
呵呵,不是说不用group by 么?
--------------
05可以
azhhuoiu 2008-06-04
  • 打赏
  • 举报
回复
declare @tb table (x varchar(10),y varchar(10))
insert into @tb select 'a1' ,'a'
union all select 'a2', 'a'
union all select 'a3', 'a'
union all select 'b1', 'b'
union all select 'b2', 'b'
union all select 'a1', 'c'
union all select 'a2', 'c'
union all select 'a3', 'c'
union all select 'b1', 'd'
union all select 'a1', 'd'
union all select 'a2', 'd'
union all select 'a3', 'd'
union all select 'e1', 'e'
union all select 'a1', 's'

select y ,count(x) from @tb group by y HAVING COUNT(CASE WHEN x IN('a1','a2','a3') THEN 1 ELSE NULL END)=3 and count(x)=3
中国风 2008-06-04
  • 打赏
  • 举报
回复
select 
[厂家]
from
T a
where
not exists (Select 1 from T b where [厂家]=a.[厂家]
and not exists(select 1 from T where [产品]=b.[产品] and [产品] in('a1','a2','a3')))
group by [厂家]
having count(1)=(select count(distinct [产品]) from T where [产品] in('a1','a2','a3'))
hery2002 2008-06-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 roy_88 的回复:]
select
[厂家]
from T a
where [产品] in('a1','a2','a3')
group by [厂家]
having count(1)=(select count(1) from t where [厂家]=a.[厂家]) and count(1)=3
[/Quote]
呵呵,不是说不用group by 么?

hery2002 2008-06-04
  • 打赏
  • 举报
回复
.....
中国风 2008-06-04
  • 打赏
  • 举报
回复
count(1)=3--取传参记录
中国风 2008-06-04
  • 打赏
  • 举报
回复
select 
[厂家]
from T a
where [产品] in('a1','a2','a3')
group by [厂家]
having count(1)=(select count(1) from t where [厂家]=a.[厂家]) and count(1)=3
internetroot 2008-06-04
  • 打赏
  • 举报
回复
我的意思是用型如:

select ......
in ('a1','a2','a3')

的方式查询出来
中国风 2008-06-04
  • 打赏
  • 举报
回复
传参时有没有引号的'b1','b2'
wuxinyuzhu 2008-06-04
  • 打赏
  • 举报
回复
呵呵 帮D

22,206

社区成员

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

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